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:
► while
► do-while
► 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(“ABC ”);
i++;
}
printf(“Out
of loop”);
}
Output:
ABC ABC ABC ABC ABC
In this example, printf is executed 5 times. You have to think
about three things to control loop execution. They are initialization,
termination condition and flow. Here, i is used to control loop. In the very
first line of the main function i is initialized by 1. So when the condition is
evaluated first time it is interpreted as 1<=5, thus it is TRUE. Control
enters in while block and execute printf statement. After printf i is
incremented by 1. This is important as we want to execute loop body only five
times. Initially i was 1 and in each iteration it is incremented by 1, this
makes i to reach a value where condition i<=5 becomes FALSE.
Syntax of do-while loop
main()
{
….
….
do
{
….
….
} while(condition);
…
}
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( ; ;)
{
…
}
…
}
for loop is programmers choice as it contains
initialization, termination and flow at same place. Notice the two semicolons
inside for’s 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:
► Loop body
► Switch
body
break in loop body
The keyword break is used in loop body to terminate loop.
When break executes, it transfers the control out of loop body.
Whenever you are not sure with when the loop should stop,
you can use break to terminate loop.
Example:
main()
{
int x,i=1;
while(i<=5)
{
printf(“Enter
an even number”);
scanf(“%d”,&x);
if(x%2==0)
{
printf(“You
Win”);
break;
}
i++;
}
if(i==6)
printf(“You
lost”);
}
In this program, break is used to terminate loop when user
enters an even number. This program is a sort of game where user has at most 5
chances to enter correct value. If he is fail to enter correct value in all
five chances he would lost the game. He may win the game if he enters correct
value in any of the five chances. Game must stop as soon as he enters the
correct data.
Let us assume first time user enters 3. Since x contains 3,
condition x%2==0 is FALSE so skipped if block and increments i by 1. On its
second iteration, assume user enters 6. This time condition x%2==0 is TRUE, so
enters in if block. User gets a message “You Win”. Now break works and it
transfers the control out of while block. Where another condition is evaluated,
which is false.
Try executing above program for different inputs.
Keyword continue
The keyword continue is used only in loop body. It is used
to move control at termination condition in the case of while and do-while
loop. continue is used in for loop to transfer control at flow part
(increment/decrement).
Example
main()
{
int x;
while(1)
{
printf(“Enter
an even number”);
scanf(“%d”,&x);
if(x%2==1)
continue;
else
{
printf(“This
is the correct value”);
break;
}
}
}
This time condition of while loop is always TRUE as we wrote
1 which is a non zero number. (Non zero number is always treated as TRUE). The
loop only ends at the execution of break. If the user enters an odd number
condition x%2==1 becomes TRUE, thus continue works. Continue transfers the
control at while loop condition.
As long as user enters odd number, continue works every
time, but when user enters an even number, break terminates loop.
No comments:
Post a Comment