Professional Documents
Culture Documents
chapter 5 h
chapter 5 h
The statement which alter flow of execution of a program are known as control statements. In the
absence of control statements, the instructions or statements are executed in same order in which
they appear in source program. Such type of construct is known as sequential construct.
Sometimes tasks are performed on the basis of certain logic test where output comes according
to true or false tests or conditions. Similarly , sometimes it is necessary to perform repeated
actions or skip some statements from execution. For these operations, control statements are
needed. They control flow of program so that it is not necessary to execute statements in the
same order in which they appear in the program.
1. Sequential
2. Selective (Branching)
a) Two way branching : if...else
b) Multiple branching : if...else if...else and switch
3. Repetitive (Looping)
a) Entry controlled : while and for
b) Exit controlled : do...while
1. Sequential Structure : Sequence simply means executing one instruction after another, in the
order in which they occur in the source file. This is usually built into languages as the default
action, as it is with C. If an instruction is not a control statement, then the next instruction to be
executed will simply be the next one in sequence.
if statement
switch statement
if statements
The if statement is a powerful decision making statement and it is used to control the flow of
execution of statements. It is a two way statement and is used in conjunction with a test
expression. Syntax: if(test expression)
if statement allows the computer to evaluate the expression first and then depending on whether
the value of the expression (condition) is true or false, it transfer the control to a particular
statement. The if statement may be implemented in different forms depending on the complexity
of conditions to be tested. Which are :
Simple if statement
if...else statement test expression False
Nested if...else statement
?
else...if ladder statement
True
Simple if statement : The simple if statement is used to execute a block of code conditionally
based on whether the given condition is true or false. If the condition is true , the block of code
will be executed, otherwise it will be skipped.
if (test expression)
{
statement-block;
}
statement-x;
Figure : general form and flowchart of
simple if statement.
If the test expression is true the statement-block will be executed otherwise the statement-block
will be skipped and the execution will jump to statement-x. but when the condition is true both
the statement-block and the statement-x are executed sequence.
The if...else selection structure : The if...else statement extends the idea of the if statement by
specifying another section of code that should be executed only if the condition is false i.e.,
conditional branching. The form is:
if(test expression)
{
true block statement(s)
}
else
{
false block statement(s)
}
Fig : Syntax and Flowchart
Here , test expression is an expression, that gives true or false values, true-block statement(s) are
to be executed only if the test expression is true and false-block statement(s) is to be executed
only if the condition is false.
Example: WAP to ask the user three sides of a triangle and display the area. Before computing
area check whether the triangle is formed by the given sides or not.
void main()
{
float a, b, c, s, area;
printf("Enter the length of three sides of a triangle: ");
The nested if.......else statement : When a series of decision are involved, we may have to use
more one if...else statement in nested form. The ANSI standard specifies that 15 level of nesting
may be continued. In c, an else statement in the same block and not already associated with an if.
Syntax: if(test-expression1)
{
if(test-expression2)
{
statementblock-1;
}
else
{
statementblock-2;
}
}
else
{
statementblock-3;
}
Example : WAP to read integer and find its square if it is less than 50 else check whether it is
odd or even and display appropriate message.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n, r; //n for integer and r for result.
printf("Enter value of n: ");
scanf("%d", &n);
if(n>0)
{
if(n<50)
{r=pow(n,2); printf("Square of n is %d", r);}
else
{ if(a%2==0)
{printf("%d is even", n);}
else
{printf("%d is odd", n);}
}
}
else
{ print("invalid input");}
getch();
}
Switch statement : C has built a multi way decision statements known as switched, that tests the
value of an expression against a list of case values (integer or character constants). When a
match is found, the statements associated with that case is executed.
Syntax:
switch (expression)
{
case constant1:
block of case constant1;
break;
case constant2:
block of case constant2;
break;
case constant3:
block of case constant3;
break;
.................................................
.................................................
default:
Q. WAP that to determine all roots of a quadratic equation by using switch statement.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, r1, r2, discriminant, realpart, imaginarypart;
int switchexpression;
printf("Enter value of a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant=pow(b,2)-4*a*c;
y={2x+300 for x<50, 200 for x=50 and 50x-100 for x>50}
#incude<stdio.h>
#include<conio.h>
void main()
{
float x, y;
printf("Enter value of x: ");
scanf("%f", &x);
y=(x!=50)?((x<50)?(2x+300):(50x-100)): 200;
printf("y = %f", y);
getch();
}
a) while
b) do...while
c) for
a) while statement : It specifies that a section of code should executed while a certain condition
holds true.
Syntax & flowchart :
while(test_expression)
{
body of loop
}
Here, test condition is evaluated first and if it is true (non-zero), then the body of loop will be
executed. After execution of body, the test condition is once again evaluated and if it is true, the
c) for statement : Both while and do...while statements provide repetition until some condition
becomes false.
for statement is used to execute a block of code for a fixed number of repetition.
Syntax & flowchart :
for(initialization;test_expression;update_expression)
{
body of loop.
}
Nesting of loops
Putting one loop statement within another loop statement is called nesting of loop.
a) Nesting of for loops : Putting one for statement within another for statement is called nesting
of for loops.
general form : for(initialization;test_expression;update_expression)
{
for(initialization;test_expression;update_expression)
{
.......................
.......................
}
}
Nesting may be continued up to 15 levels in ANSI C. Many compilers allow more than 15 levels.
Q. WAP to display the multiplication table of m by n.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows,columns;
printf("Enter row and column size : ");
scanf("%d%d", &rows, &columns);
for(i=1;i<=rows;i++)
{
for(j=1;j<=columns;j++)
{
printf("%d\t",i*j);
}
printf("\n");
}
}
b) Nesting of while loops
general form : while(test_expression)
{
while(test_expression)
{
............................
............................
Entry Controlled Loop (pre-test loop) & Exit Controlled Loop (post-test loop)
Entry Controlled Loop Exit Controlled Loop
Test condition appears at the beginning of Test condition appears at the end of loop.
loop.
Control variable is counter variable. Control variable is counter and sentinel
variable.
Each execution occurs by testing condition. Each execution except first the first one occurs
by testing condition.
for and while loop belongs to entry do...while loop belongs to exit controlled loop.
controlled loop.
Example: ------------ Example: ------------------------
Sum=0; do
N=1; {
while(n<=10)
{ printf("Input a number: ");
sum= sum + pow(n,2); scanf("%d",&num);
n=n+1; }while(num>0);
} --------------------------
----------------------
In the while or do...while loop, the next iteration starts by re-evaluating the test condition.
Depending on the result, the loop either terminates or another iteration occurs.
In the for loop (using the syntax for(initialization; condition; update)), continue causes update
expression to be executed. Then test condition s re-evaluated and depending on the result, the
loop either comes to an end or begins next repetition.
Example : WAP to print the alphabetic characters and their ASCII values in range from 65 to
122. But there are non-alphabetic character from 91 to 96 which are skipped using continue
statements.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=65; i<=122; i++)
{
if(i>90 && i<96)
{
continue;
}
printf("%c=%d",i,i);
Each label statement within the program must have a unique label.
Label can be before or after the goto statements.
goto breaks the normal sequence of program.
Backward jump : If the label: is before the statement goto label; a loop will be formed
and some statement will be executed repeatedly. Such a jump is called as backward jump.
General form: label:
.......................
.......................
goto label;
statements;
Forward jump : If the label: is after the goto label; statements, some statements will be
skipped and the jump is called as a forward jump.
General form: goto label;
..................
..................
label:
statements;