For Loop Example - Java Programme.



For loop executes group of Java statements as long as the boolean condition evaluates to true.

For loop combines three elements which we generally use: initialization statement, boolean expression and increment or decrement statement.

The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable.
Condition statement is evaluated before each time the block of statements are executed. Block of statements are executed only if the boolean condition evaluates to true.

Statement is executed after the loop body is done. Generally it is being used to increment or decrement the loop variable.

Following example shows use of simple for loop.

Programme :

Output :
Value of i : 1
Value of i : 2
Value of i : 3
Value of i : 4
Value of i : 5

Note : However it is not possible to include declaration and initialization in the initialization block of the for loop.


Follow me on:

Comments