Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!

# For Statement

(for is a keyword)
To repeat one or more statements a certain number of times, normally we use for statement.

Syntax:
for(initialization; condition; increment/decrement)
{
(statements to be executed are typed here)
}

Where,
initialization: will be expression of the form i=1, sum=0, etc.
condition: will be expression whose value is true or false, e.g.: i<=10
increment/decrement: an expression of the form i++,i--,etc.
statements: any valid C++ statement.

Example 1:

Example 2:

Example 3:

Example 4: Initialization can be omitted from the for syntax as mentioned below

Example 5: Increment/decrement can be omitted from the for syntax as mentioned below

Example 6: Condition can be omitted from the for syntax as mentioned below. Without condition the loop will execute infinite times.

Example 7: More than 1 initialization and increment/decrement possible.

Example 8: For statement to be repeated only once

Example 9: Increment/decrement can be written in 2 ways

## Nested for loops:

Example 1: One or more for loops inside another for loop is known as nested for loop. For each value of i, a=a+b repeats 10 times.

Example 2: For each value of i, a=a+b and c=c+d repeats 10 times

Example 3: For each value of i, a=a+b repeats 10 times but c=c+d repeats only once as it is outside the second loop.

CACKLE comment system