Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter-6
Iterative Control Instruction
Loop
Iterative control instruction is also known as repetitive control instruction or loop.
Sometimes it is desirable to executed same statement again and again. This can be done
with the help of loops.
There are three ways to implement loops in C language:
l while
l do-while
l for
Syntax of while
main()
{
.
.
while(condition)
{
.
.
}
}
Syntax of while is similar to if. In the case of if, when the condition is TRUE control
moves inside if block and execute statements of if-block. After executing if-block control
moves to the statement written immediately after if-block (outside if-block).
In the case of while, when the condition is TRUE control moves inside while-block and
execute statements of while-block. After executing while-block control does not moves
to the statement written immediately after while-block rather it goes back to the condition
of while block. This condition will be checked again and if it is again TRUE control
moves again inside while-block. This repeats till the condition becomes FALSE.
Example:
main()
{
int i=1;
.
while(i<=5)
{
printf(SCA );
Learn a new keyword do. The code block that has to put in loop is prefixed with keyword
do and post-fixed with keyword while along with termination condition. Remember to
put semicolon after while.
Do-while works similar to while but the only difference is earlier is executed at least
once. Since in while loop condition is evaluated first then goes into loop body, on the
other hand in do while loop first control enters in loop body then condition will be
checked. This makes possible to control enters in loop body even though the condition is
false. Although once the condition is checked FALSE loop is terminated.
Syntax of for
main()
{
}
for loop is programmers choice as it contains initialization, termination and flow at same
place. Notice the two semicolons inside fors round braces, these are part of syntax,
hence should always be mentioned.
Two semicolons create three sections. First section is used for initialization, second
section is used for termination condition and third section is used to mention flow.
Example: Program to calculate factorial of a number
main()
{
int n,i,f=1;
printf(Enter a number );
scanf(%d,&n);
for(i=1 ; i<=n ; i++)
{
f = f * i;
}
printf(Factorial is %d, f);
}
Output
Enter a number 4
Factorial is 24
User enters a number which is stored in n. This loop executes n times as it goes from 1 to
n. Let us suppose value entered by user is 4. So n contains 4. Loop start with initialization
of i from 1. After initialization, termination condition is evaluated, which is TRUE, thus
control moves inside the code block. Initially f is containing 1 and it is multiplied by i
which also contains 1. The product is finally stored in f. Now control moves to the flow
part (i++). Condition is evaluated again but this time i become 2. Condition is again
TRUE. Control again enters in code block and the product of f and i will store in f.
Same process is repeated till the condition i<=n becomes FALSE.
Keyword break
The keyword break is used only in:
l Loop body
l Switch body