5,7.control Statements
5,7.control Statements
In POP, program is divided into small parts called functions. In POP, Importance is not given to data but
to functions as well as sequence of actions to be done. POP follows Top Down approach. POP does not
have any access specifier. In POP, Data can move freely from function to function in the system. To add
new data and function in POP is not so easy. In POP, Most function uses Global data for sharing that can
be accessed freely from function to function in the system. POP does not have any proper way for hiding
data so it is less secure. In POP, Overloading is not possible. Example of POP are : C, VB, FORTRAN,
Pascal.
When variables and constants of different types are combined in an expression then they are converted to
same data type. The process of converting one predefined type into another is called type conversion.
When the type conversion is performed automatically by the compiler without programmers intervention,
such type of conversion is known as implicit type conversion or type promotion.
The compiler converts all operands into the data type of the largest operand.
The sequence of rules that are applied while evaluating expressions are given below:
1. If either of the operand is of type long double, then others will be converted to long double and
It should be noted that the final result of expression is converted to type of variable on left side of
assignment operator before assigning value to it.
Also, conversion of float to int causes truncation of fractional part, conversion of double to float causes
rounding of digits and the conversion of long int to int causes dropping of excess higher order bits.
The type conversion performed by the programmer by posing the data type of the expression of specific
type is known as explicit type conversion.
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant, variable or expression.
For example,
1 x=(int)a+b*d;
The following rules have to be followed while converting the expression from one type to another to
avoid the loss of information:
(a) getchar()
(b) putchar()
getchar(): This function is used to read a single character from the standard input device i.e.,
keyboard.
charactervariable=getchar();
Example: char ch;
printf(“Enter a character”);
ch=getchar();
putchar(): This function is used to display a single character on the standard input device i.e.,
monitor.
The general syntax of putchar() function is
putchar(charactervariable);
Example: char ch;
printf(“Enter a character”);
ch=getchar();
putchar(ch);
Formatted i/o functions:
(a) scanf()
(b) printf()
scanf(): This function is used to read any kind of data from the standard input device.
Here, the control string contains the format specifications (%d %c etc) and the argument
list contains the addresses of variables that stores the actual data (&n, &ch)
Here, the control string contains the format specifications (%d %c etc) along with some
message (optional) and the argument list contains the set of variables to be displayed onto the
monitor.
The list of format specifiers used in ‘c’ language are given below
%d or %i or %n For integers
%c For characters
puts(str);
Escape sequences / back slash character constants:
Escape sequences in ‘c’ language are the character representations that may appear either in a
character constant or in a string constant.
An escape sequence always begins with a back slash (\) followed by one or more special
characters.
Description
Escape Sequence
\n New line character
\\ Single \
%% Single %
\b Backspace
\v Vertical tab
\f Form feed
\r Carriage return
\” Double quotes
\’ Single quote
\\ Back slash
\? Question mark
\0 null
Control statements:
Control statements are used to jump from one location to another location in a program or to
execute a set of statements repeatedly.
The control statements in ‘C’ language can be broadly classified into two categories.
1. Conditional control statements
2. Unconditional control statements
Conditional Control statements:
There are 2 types of conditional control statements in ‘C’ language
1. Selection control statements
2. Looping control statements
Selection control statements:
There are 5 kinds of selection control statements in ‘C’ language.
1. Simple if (1-way decision control statement)
2. If-else (2-way decision control statement)
3. Nested if-else
4. Else-if ladder
5. Switch
Simple if:
The “simple if” is used to test simple conditions.
Working:
If the condition is true then the set of statements in the if block will be executed.
If the condition is false, then the control jumps to the statement which is the first
statement after the if block.
The general form of simple if follows:
if (condition/expression)
{
n statements;
}
Statement n+1;
The behavior of simple if statement can be described by using a flowchart as given below.
The if-else statement is a 2-way decision control statement. A condition is tested at the beginning
of if-else block.
Working:
If this condition is true, then the set of statements in if block or true block gets executed.
If this condition is false, then the set of statements in else block or false block gets
executed.
After executing either if lock or else block, the control jumps to the statement which is
the first statement after the if-else statement.(statement n+1)
The general form follows:
if(condition/expression)
{
// true block
set of statements;
}
else
{ //false block
set of statements;
}
Statement n+1;
The behavior of simple if else statement can be described by using a flowchart as given below
Nested if-else :
Placing one if-else statement inside another if-else statement is called nested if-else statement.
The working of nested if-else statement is same as the if-else statement.
The general form follows:
if(condition1/expression)
{
if(condition2/expression)
{
set of statements;
}
else
{
set of statements;
}
The } behavior of nested if else statement can be
described by using a flowchart as given
below else
{
if(condition3/expression)
{
set of statements;
}
else
{
set of statements;
}
}
Statement n+1;
condition
false Condition1 true
false true
Else-if ladder:
Statements
The else-if ladder isinaelse block decision control statement. Statements in if block
multiway
if-else block(condition3) If-else block(condition2)
If condition1 is true, then the set of statements in if block gets executed and the control
goes out of the else if ladder and executes the first statement after the else-if ladder (statement
n+1).
If condition1 is false, then condition2 will be tested. The above process is repeated until any
of the conditions in the else-if ladder is true.
Statement n+1;
Statement n+1;
If all the conditions are false, then the set of statements in else block gets executed.
The general form follows:
if(condition1/expression)
{//block1
set of statements;
}
else if(condition2/expression)
{//block2
set of statements;
}
else if(condition3/ expression)
{//block3
set of statements;
}
:
else
{//else block
set of statements;
}
Statement n+1;
Switch:
Switch is also a multiway decision control statement which can be used as an alternative for else-
if ladder.
If the value of variable or expression matches with a case value then the set of statements in
the corresponding block gets executed. If there is no match with any of the case values then the
set of statements in default block gets executed.
The “break” statement in the switch causes the control to go out of the switch statement. The
default statement can be placed anywhere within the switch.
The general form follows:
switch (expression)
{
case constant1:
set of statements;
break;
case constant2:
set of statements;
break;
case constant3:
set of statements;
break;
………………………
default:
set of statements;
}
The behavior of switch statement can be described by using the flowchart given below.
Variable/expression
Value1 No matching
Statement n+1;
Example:
printf(“Enter day value”);
scanf(“%d”,&day);
switch(day)
{
case 1:
printf(“Sunday”);break;
case 2:
printf(“monday”);break;
case 3:
printf(“tuesday”);break;
case 4:
printf(“wednesday”);break;
case 5:
printf(“thursday”);break;
case 6:
printf(“friday”);break;
case 7:
printf(“Saturday”);break;
default:
printf(“Illegal choice”);
}
Initialization statement;
while(condition/expression)
set of statements;
increment/decrement
statement;
Next_statements;
The behavior of while loop can be described by using a flowchart given below.
Initialization statement
condition
false true
Example: I = 5;
while(I<=10)
{
printf (“%d”,I);
I++;
}
Do while loop:
The do-while loop is an “ exit controlled loop “ which may be executed one or more times.
Working:
The loop control variable (lcv) must be initialized before the start of do-while loop,
Then the control directly enters into the body of do-while loop and executes all the
statements in the body.
Then the condition will be tested at the end of do-while loop.
If the condition is true , then the set of statements in the body of do-while loop will be
executed.
If the condition is false, then the control goes out of the do-while loop (goes to statement
n+1;)
.
The general form follows:
Initialization statement;
do
{
set of statements;
increment/decrement statement;
}while(condition/expression);
Initialization statement
condition
false true
Next_statement
Example: I = 5;
do
{
printf (“%d”,I);
I++;
} while(I<=10);
Note: No other conditional control statement should be terminated by a semicolon.
For loop:
The “for” loop is an “entry controlled loop” which may be executed for zero or more times.
Note : In for loop, updation of loop control variable, conditional testing and initialization of loop
control variable are written at one place.
Working:
In for loop, initialization of loop control variable is done at the beginning of “for” loop,
Then the control goes to condition testing.
If the condition is true, then the set of statements inside the body of “for” loop gets
executed and the control will go to the updation of loop control variable.
After updation, the condition will be tested again.
The set of statements in the body of “for” loop will be executed until the condition
becomes false.
Whenever, the condition is false, the control will go out of the for loop (statement n+1;)
Example
ample:
1. for(x = 1;x<=10;x++)
printf(“%d”,x);
2. for(x=10;x>=1;x---)
printf(“%d”,x);
The behavior of “for” loop can be described by using a flowchart given below.
expression1
expression2
false true
Next_statement
expression3
any loop
{
Statement1;
Statement 2;
:
break;
:
}
next statement
When a break statement in a nested loop, then it can break only one loop in which it is present.
Example:
output
I = 0; I=0
while(I<10) Hello
{ I=1
printf (“I=%d”,I); Hello
I++; I=2
if(I==4) Hello
break; I=3
printf(“hello”);}
Continue:
The continue statement is a jump statement which can be used to skip a set of statements
in the loops and continue with the next iteration.
The syntax of continue statement is
continue;
any loop
{
Statement1;
Statement 2;
:
continue;
:
}
next statement
The continue statement must be used inside a loop control statement only. When a continue
statement is written in a nested loop, then the control will be transferred to the condition testing
of the loop in which the continue statement is present.
Example:
output
goto statement:
The goto statement is a jump statement which is used to transfer the control from one
location to another location within a ‘c’ program.
The syntax of goto statement is
goto labelname;
label:
{
Statement1;
Statement 2;
:
}
:
goto label;
The goto statement can also be used to construct loops by using the backward jumps. The
use of goto statement in a ‘c’ program produces inefficient machine language code. So, it is
recommended to avoid the use of goto statement in ‘c’ programs.
If the block of statements that has label appears before the goto statement, then the
control has to move to backward and that goto is called as backward goto. If the block of
statements that has label appears after the goto statement, then the control has to move to
forward and that goto is called as forward goto.
Example: