for loop


for loop

For loop is most widely looping construct in Java and is used to iterate over set of statements based on controlling expressions

Syntax:

for (initialization; condition; iteration) {
// body of for loop
}

Working:

Initialization: Initialization is executed only once. Initialization is the first statement to be
executed. It is the beginning of the execution of any control statement.
Condition: The condition is evaluated as a true or a false, it is generally a Boolean expression.
If the condition is true, then the loop body is executed.
If the condition is false, then the loop is altogether terminated
the loop control variable increment or decrement is handled by the Iteration.

The for loop work as initialization as its first step, followed by which the checking of condition
and then the increment or the decrement is carried out on. This loop continuation is an iterative process. It gets ended up once when the condition fails

Nested For loop:

Nested for loop is a condition if we have one loop inside another loop

Example No.1:

Com.knowledge2life public class Main { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println("Knowledge2life"); } } }

OUTPUT:

Knowledge2life
Knowledge2life
Knowledge2life
Knowledge2life
Knowledge2life