Iterative Control Instruction(Loops) in C Language


Iterative Control Instruction (Loops)




Loops

Iterative control instruction is also known as repetitive control instruction or 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 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 statement 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


while loop in c,while loop program in c
example of while loop in c



OUTPUT --


balendrabalendrabalendrabalendrabalendra

In this example, prinft 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 becomes FALSE. If no termination condition is defined under while loop you get trapped in an infinite loop.


do while loop




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.



do...while loop Syntax



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


}
Example 

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

getch();
return(0);
}

OUTPUT --
balendra
NOTE:-  Condition i>6 is false but before checking this condition, printf is executed once.

for Loop


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




Comments

Popular posts from this blog

Notepad, Wordpad and Paint

HOW TO PASS CCC EXAM WITH ONE ATTEMPT (NIELIT)

Inheritance in C++ Language(part 1)