Chapter 7. Loops.doc
Chapter 7. Loops.doc
LOOPS
⮚ while statement
⮚ do-while statement
⮚ for statement
⮚ break and continue statement
⮚ goto statement
⮚ nested loops
Loops
The loop is powerful statement in programming. The loops are used for repetitive
execution of statement in block of loop as long as the condition is true, all statement between
blocks of loops are executed repeatedly. When looping condition becomes false, the control
moves to the next statement immediately after the end of loop block. Where the condition is
valid logical statement. The logical statement determines the condition under which the loop
should continue repeating.
1. while loop
While (condition)
{
Body of while;
}
In this loop, the condition is given at the top. ‘while’ statement tests the values of
expression before processing the body of ‘while’. Then comes the body of ‘while’. The
body of ‘while’ may have one statement or more than one statements. If there is single
statement no need for { }. If there are more than one statements then brackets are
compulsory.
2. do – while loop
This loop is used when the statements are to be executed initially and the result
of statement is compared. In case if the condition is satisfied or true, the looping is
continued otherwise looping is terminated.
In this loop computer enters into the body of do-while execute it and then checks
the conditions. If conditions is true it goes back and continues till condition remains true
when condition becomes false it goes to next statement.
do
{
body of loop;
}
while (condition);
E.g. do
{ printf(“%d \n”, n);
n++; }while (n<=100);
In the above example the statement is executed 100 times. Initially the value of n is
displayed and then incremented by ne until the value of n is equal to 100. When the
value of n becomes 100 then the execution comes out of loop and goes to the next
statement.
While do-while
1. Condition is on top 1. condition is at bottom
2. No necessity of brackets if there 2. Brackets are compulsory even if
is single statement in body there is a single statement.
3. Computer executes the body if condition 3. Computer executes the body at
is true. least once even if condition is
false.
4. This should be used when condition is more 4. This should be used when the
important. process is important.
5. This loop is also referred as entry control loop. 5. This loop is also referred as exit
controlled loop.
6. While (n<=100) 6. Do
{ printf(“%d\n”, n); { printf(“%d\n”,n);
n++; n++;
} }while (n<=100);
E.g. for(i=1;i<=100;i++)
for(n=1;i<=50;n++)
for(i=2;i<=200;i++)
Here everything is specified in the same line and hence we can tell the number of
repetitions it can take. When we know the number of repetitions in advance then we should go
for the ‘for’ loop. This loop can be written as follows:
{ i++; }
3) for (i=1;1<=100)
{ i++; }
4) for(i=1; j=10;i<=10;i++;j--)
We can initialize, increment/ decrement more than one variable in for statement.
5) ( ; ; )
main()
int i;
for (i=1;i<=100;i++)
printf(“%d\n”, i);
main()
int i,k,d;
scanf(“%d”, &k);
d=1;
for (i=1;i<=k;i=i+d)
printf(“%d\n”, i);
d=d+1;
Goto statment
This statement transfers the control of the computer from one statement to other
statement in the program which may not be in sequence.
ii) Conditional.
i) Unconditional
The format : goto label; where label is any valid identifier which indicates
the statement to which control should be
transferred.
ii) Conditional
When goto statement is written as body of if or if-else .i.e., with some condition
then it is called conditional goto. Here computer checks the condition, if the condition is
true it executes goto. By putting proper condition we can control the number of
repetitions.
goto label;
e.g. if (n<=100)
goto top;
if (a<b)
goto bottom;
It is never advised to use goto statement in programming, still if you want to use then always go
for conditional goto.
Nested loops
⮚ When one loop is inserted completely within another loop then it is called nested loop.
⮚ Nested loop means in one loop statement (inner loop) is executed inside the other loop
statement (outer loop).
⮚ The statement provides more number of cycles of executions because for every value of
outer loop statement is varied, the value of inner loop statement is also varied.
The nested loop can have form like these figures.
When the nested loop is executed then for every increment in the outer loop the inner loop
takes all its values. Here the execution gets multiplied. Hence nested loops are powerful, useful.
E.x. main()
{
int i,j;
for (i=1;i<=3;i++)
{
for(j=1;<=3;j++)
{
printf(“%d %d \n”, i, j)
}
}
}
The output :
11
22
13
21
22
23
31
32
33