Professional Documents
Culture Documents
Unit - Ii Control Structures
Unit - Ii Control Structures
Sequential control:
In sequential control, the C program statements are executed sequentially i.e., one after the
another from beginning to end.
2
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
3
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
utput:
O
Enter two numbers:
43
54
The number 54 is big.
utput:
O
Enter three numbers:
22
55
4
Largest number is 55
5
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
4.Else if ladder:
Thisisatypeofnestinginwhichthereisanif-elsestatementineveryelsepartexceptthelast
else part. This type of nesting is called else if ladder.
The general syntax and flowchart of else if ladder is as follows:-
if(condition1)
statementA;
else if(condition2)
statementB;
else if(condition3)
statementC;
else
statementD;
utput:
O
Enter the percentage of the student:89
A grade
utput:
O
Enter the day number from 1 to 7 : 6
saturday
OTE:Inswitchcasestatements,ifwedonotassociateabreakstatementwitheverycasethenfrom
N
thecaseforwhichtheexpressionmatcheswiththeconstant,allthestatementsareexecuteduntilthe
switch case block ends. This situation is referred to as Fall Through.
Iterations/Loops
Iterationsorloopsareusedwhenwewanttoexecuteastatementorblockofstatementsseveral
times.
The repetition of loops is controlled with the help of a test condition. The statementsinthe
loop keep on executing repetitively until the test condition becomes false.
There are the following three types of loops:-
8
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
. While loop
1
2. Do-while loop
3. For loop
ll these loops are explained as under:
A
while loop :
I tisthefundamentalloopingstatementinC.Itissuitedforproblemswhereitisnotknownin
advance how many times a statement or block of statements will be executed.
The generalsyntaxof while loop is as under:
Initialization;
while(condition)
{
Statement(s)
Increment / Decrement;
}
We can able to understand theworking of while loopwith the help of flow chart below:
. F
1 irstly theconditiongiven withwhileis evaluated.
2. If the condition evaluates to true then the statements given in the body of the loop are
executed.
3. Aftertheexecutionofallthestatementstheconditionisagaincheckedandifitagainevaluates
to true then the statements given in the body of the loop are again executed.
4. Inthiswaythestatementsareexecutedagainandagainuntiltheconditiongivenevaluatesto
false.
do-while loop :
➢
The do-while statement is also used for looping.
➢ Itissimilartowhileloopandisusedforproblemswhereitisnotknowninadvancehowmany
times the statement or block of statements will be executed.
➢ However,unlikewhileloop,incaseofdo-while,firstlythestatementsinsidetheloopbodyare
executed and then the condition is evaluated.
➢ As a result of which this loop is executed at least once even if the condition is initially false.
➢ After that the loop is repeated until the condition evaluates to false.
➢ Sinceinthislooptheconditionistestedaftertheexecutionoftheloop,itisalsoknownasthe
post test loop.
10
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
he general syntax of do-while loop is as follows:
T
Initialization;
do
{
Statement(s)
Increment / Decrement;
}while(condition);
for loop :
he general syntax of for loop consists of three expressions separated by semicolons. It is
T
given as follows:-
12
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
f or(initialization;termination condition;increment / decrement)
{
Statement(s);
}
1. I nitialization is executed only once when the loop starts and is used to initialize the loop
variables.
2. Termination conditionis a condition and is testedbefore each iteration of the loop.
3. Increment / decrement is an update expression used to update the loop variable and is
executed each time after the body of the loop is executed.
he flow chart of for loop is given as follows:
T
utput:
O
1
2
3
4
5
13
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
2 . Example program forforloop
/* program to generate the Fibonacci series */
/* The Fibonacci sequence is a sequence where the nexttermisthesumoftheprevioustwoterms.
The first two terms of the Fibonacci sequence are 0 followed by 1.*/
#include<stdio.h>
int main()
{
int x,y,z;
int i,n;
x=0;
y=1;
printf("Enter the number of terms : ");
scanf("%d",&n);
printf("\n%d",x);
printf("\n%d",y);
for(i=1;i<n;i++)
{
z=x+y;
printf("\n%d",z);
x=y;
y=z;
}
}
Output:
Enter the number of terms : 5
0
1
1
2
3
5
● A llthethreeexpressionsintheforloopareoptionalandthereforewecanomitanyorallthe
three expressions but in any case the two separating semi colons should always be present.
● Ifweomitthetestexpressionthenitisalwaysassumedtobetrueandthereforetheloopnever
terminates and becomes an infinite loop.
● Ifwewanttomakesuchloopsafiniteloopthenaseparateterminateconditionisgivenwithin
the loop body
Jump statements (or) Unconditional control transfer statements
J ump statements are used to transfer the control from one part of the program to another part.
The various jump statements in C are as under:
1. break statement
2. continue statement
3. goto statement
1. break statement:
14
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
hebreakstatement is used inside the loops or switch statement. This statement causes
T
an immediate exit from the loop or the switch case block in which it appears.
It can be written as
break;
15
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
2. continue statement :
hecontinuestatementisusedinsidethebodyoftheloopstatement.Itisusedwhenwewant
T
to go to the next iteration of the loop after skipping some of the statements of the loop.
The continue statement is written as under:
continue;
oto label;
g
...................
...................
label:
....................
....................
yntax 2:
S
label:
...................
...................
goto label;
....................
....................
➔
Here label is any valid C identifier and is followed by colon.
➔ The goto statement transfers the control to the statement after the label.
➔ If the label is placed after the goto statementthenthecontrolistransferredforwardanditis
known as forward jump. If the label is placed before the goto statement then the control is
transferred backward and the jump is called backward jump.
➔ In the forward jump all the statements between the goto statement and the label are skipped.
➔ In case of backward jump all the statement between the goto statement and the label are
executed.
➔ There should always be a statement after any label.
Example program forgoto
#include<stdio.h>
int main()
{
int a;
printf("\nEnter a value : ");
17
ITP Notes for I B.Tech CSM - A, SVCET, Chittoor
s canf("%d",&a);
printf("Hello");
if(a == 0){
goto label;
}
printf("World");
label:printf("Exit");
}
Output 1:
Enter a value : 9
HelloWorldExit
Output 2:
Enter a value : 0
HelloExit
18