Monday, 25 February 2013

for loop


C programming loops:
Loops causes program to repeat the certain block of code until some conditions are satisfied. Loops are used in performing repetitive work in programming.

Syntax of for loop:
for(initial expression; test expression; update expression)
{
            codes to be executed;
}

How for loops in C programming works?
The initial expression is initialized only once at the beginning of the loop. Then, the test expression is checked by the program.
If the test expression is false, for loop is terminated.
If the test expression is true then, the codes are executed and update expression is updated. Again, the test expression is checked. If it is false, loop is terminated and if it is true, the same process repeats until test expression is false.
For the easier understanding of how for loops works, analyze the flowchart of loop given below.

Important:
·         Initial, test and update expression in C for loop are separated by semicolon (;)
·         Two or more expression of same type in for loop are separated by comma(,)

No comments:

Post a Comment