For Loop In Java

For Loop ?

Loops are used in programming to repeat a specific block until some end condition is met.


How for loop works?

Iitialization of the loop variable

Test of the loop repetition condition.

Change(update) of the loop control variable.

The initialization section can be used to declare a variable.

Like a while loop, the condition of a for loop is tested prior to executing the loop body.

Therefore, the body of a for loop will execute zero or more times.

The increment section can perform any calculation.

A for loop is well suited for executing statements a sepecific number of times that can be calculated or determined in advance.

Syntax :

   for ( initialization; condition; increment/decrement; ) 
  {
   statement(s);
   }

Example 1:



Output :

Line 1
Line 2
Line 3
Line 4
Line 5



Example 2:



Output :

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3




Example 3:



Output :

  * 
  * * 
  * * * 
  * * * * 
  * * * * *