For Statement In C

For Statement In C

The for statement (loop) is most commonly used iteration construct.

We can make use of the for loop when we know the limit up to which a  loop will be executed.

The syntax of For statement is as follows,

        for(initialization ; Condition ; Increment/Decrement)
{
        Statements;
}


The execution of the for loop is as follows,

First time while the control reaches the for loop it initializes the values in the initialization section of the for loop (we can initialize multiples variables separated by a comma). Then it checks the condition following the initialization part of for loop(there can be combination of conditions) and if the condition is satisfied(True) then the statements within the loop gets executed then again the control is transferred to the increment/decrement part and it is executed .

After it again the condition is checked and if the condition is true it executes the statements again and again until the condition becomes false.

The flow chart for For loop is as follows,

for loop


Example program:



Output:


Example program 2:


Output:


This program is similar to the above program.here we have initialized the value before the for loop and increment is used with in the for loop.we can leave the initialization part or increment part if not necessary.

Example Program 3:



Output:


This program has no condition in for loop so the for loop gets executed infinite times.



Thank you …

Please show your support by subscribing to post below...
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts