Iterative control instructions

What is iterative control instruction?
    Iterative control instruction is also known as repetitive control instruction or loop.            Sometimes it is desirable to execute the same statement again and again. This can be              done with the help of loops
   Iterative control Statements are used when we want to repeat the execution of one or         more statements for the specified number of times.

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("
Balaram Gupta "); i++; } printf(“Out of loop”); return(0); }
Output
Balaram GuptaBalaram GuptaBalaram GuptaBalaram GuptaBalaram Gupta 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 the first time it is interpreted as 1<=5, thus it is TRUE. Control enters in while block and executes printf statement. After printf i is incremented by 1. This is important as we want to execute a 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 the 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(“Study For Future”);
}while(i>6);
getch();
return(0);
}

Output
Study For Future
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 the facility to write initialization, termination and flow at the same place, in the parenthesis of for. Notice the two semicolons inside for’s round braces, these are part of the syntax, hence should always be mentioned. Two semicolons created three sections. The first section is used for initialization, the second section is used for termination condition and the third section is used to mention flow.

for(initialization; condition; increment)
{
……
……
}
The execution of for loop begins with initialization, then control moves to check the 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

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals