JAVA For Loop
For Loop
is a control flow statement used to execute a block of
code repeatedly for a fixed number of iterations. It
consists of three parts.
1. Initialization – runs once before the loop starts. It
is usually used to declare and initialize a loop
control variable.
2. Condition – Loop continues as long as this
condition is evaluated to true. The loop stops when
the condition evaluated false.
3. Update or Iteration – this statement executes to
update the loop control variable.
For Loop Syntax
for (int i = 1; i <= 5; i++) {
System.out.println(“Output " + i);
}
Int i = 1; - is the initialization phase.
i >= 5; - is the condition phase.
i++ - is the iteration
Example
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
}
OUTPUT:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example
for (int i = 10; i >= 1; i--) {
System.out.print(‘’ ’’+i);
}
OUTPUT:
10 9 8 7 6 5 4 3 2 1
Activity
Create a program for Multiplication table. Allow the user input a number
from 1-10 and make a multiplication table.
Please refer to the output below.