Loops

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






while loop


int 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 move 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. while loop executes the set of statements until the condition is true or non-zero value.

Example

int main()
{
int i=1;
….
while(i<=5) { printf(“mysirg.com “); i++; } printf(“Out of loop”); return(0); }

Output
mysirg.com mysirg.com mysirg.com mysirg.com mysirg.com Out of loop

In this example, printf inside a while loop is executed 5 times. You have to think about three things to control loop. 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. If no termination condition is defined under while loop you get trapped in an infinite loop.

do while loop

Syntax of do while loop

int main()
{
….
….
do
{
….
….
} while(condition);


}


do-while works similar to while but the only difference is, earlier is executed at least once. 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.
do while loop is exit control loop, because condition is checked on exit from the block.

int main()
{
int i=5;
do
{
printf(“mysirg.com”);
}while(i>6);

getch();
return(0);
}

Output
mysirg.com
Condition i>6 is false but before checking this condition, printf is executed once.

for loop

Syntax of for loop


int main()
{
….
for( ; ;)
{

}

}

For loop provides facility to write initialization, termination and flow at same place, in the parenthesis of for. Notice the two semicolons inside for’s round braces, these are part of syntax, hence should always be mentioned. Two semicolons created three sections. First section is used for initialization, second section is used for termination condition and third section is used to mention flow.

for(initialization; condition; increment)
{
……
……
}

The execution of for loop begins with initialization, then control moves to check condition, if it is true, control enters in the body of for to execute statements and then increment part work. Again condition is evaluated, this will go on loop until the condition is evaluated as false.

Example

int main()
{
int i;
for(i=1;i<=5;i++) { printf(“\n Number = %d”, i); } getch(); return(0); }

Output:
Number = 1
Number = 2
Number = 3
Number = 4
Number = 5

Iterative Control Instruction (Loops)
Tagged on: