0% found this document useful (0 votes)
6 views4 pages

For Loop Variations Explained

The document explains the for loop, a conditional iterative statement used to execute a block of code repeatedly based on specified conditions. It outlines the general syntax, how the loop operates, and provides various forms and variations of for loops, including bodyless loops and empty loops. Additionally, it highlights the importance of initialization, condition checking, and updates within the loop structure.

Uploaded by

jajaakash95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

For Loop Variations Explained

The document explains the for loop, a conditional iterative statement used to execute a block of code repeatedly based on specified conditions. It outlines the general syntax, how the loop operates, and provides various forms and variations of for loops, including bodyless loops and empty loops. Additionally, it highlights the importance of initialization, condition checking, and updates within the loop structure.

Uploaded by

jajaakash95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FOR LOOP AND ITS VARIATIONS

For loop is a conditional iterative statement which is used to check for certain conditions and then repeatedly
execute a block of code as long as those conditions are met.

General Syntax:

for(expression1; expression2; expression3)


{
// body of for loop
statement1;
statement2;
}

The expression1 is the initialization expression.


The expression2 is the test expression or condition.
The expression3 is the update expression.

OR

for(initialization; condition; update)


{
// body of for loop
statement1;
statement2;
}

How it works:
First, the initialization expression is executed (i.e expression1 ) to initialize loop variables. The initialization
expression (i.e expression1) executes only once when the loop starts. Then the condition is checked
(i.e expression2), if it is true, then the body of the loop is executed. After executing the loop body, the program
control is transferred to the update expression (expression3). The expression3 modifies the loop variables. Then
the condition (i.e expression2) is checked again. If the condition is still true the body of the loop is executed
once more. This process continues until the expression2 becomes false.

Flow Diagram:
For Loop variations:

Form Comment

for ( i=0 ; i < 10 ; i++ ) Single statement inside for loop.


Statement1; The braces ({}) are optional and can be omitted.

for ( i=0 ;i <10; i++) Multiple statements within for loop. Curly block is
{ mandatory.
Statement1;
Statement2;
Statement3;
}

for ( i=0 ;i <5; i++) This is bodyless for loop. It is used to increment value of
{ “i”. This verity of for loop is not used generally.
At the end of above for loop value of i will be 5.
}

for ( i=0 ; i < 10;i++) ; For Loop with no Body (Carefully Look at the Semicolon )
It is called Empty Loop.

for Multiple initializations & Multiple


(i=0,j=0;i<100;i++,j++) Update Statements separated by Comma.
Statement1;

i = 0; Initialization not used.


for(;i<5;i++)
{
statement1;
statement2;
statement3;
}

i = 0; Initialization and update not used.


for(;i<5;)
{
statement1;
statement2;
statement3;
i++;
}
for(i=0;i<5;) Update not used
{
statement1;
statement2;
statement3;
i++;
}

i = 0; Test condition is not used. It becomes Infinite Loop. Never


for(;;) Terminates. Infinite for loop must have breaking condition
{ in order to break the loop. Otherwise it will cause overflow
statement1; of stack.
statement2;
statement3;

if(breaking condition)
break;

i++;
}

for(i=0; (i!=5)&&(i<10) Different Conditional expressions can be used.


; a++)
{
statement1;
statement2;
statement3;

You might also like