Java while-loop

A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:

  1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
  2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
  3. Statement: The statement of the loop is executed each time until the second condition is false.
  4. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.

Syntax:

  1. for(initialization;condition;incr/decr){  
  2. //statement or code to be executed  
  3. }  

Flowchart:

for loop in java flowchart

Example:

  1. //Java Program to demonstrate the example of for loop  
  2. //which prints table of 1  
  3. public class ForExample {  
  4. public static void main(String[] args) {  
  5.     //Code of Java for loop  
  6.     for(int i=1;i<=10;i++){  
  7.         System.out.println(i);  
  8.     }  
  9. }  
  10. }  

Test it Now

Output:

Scroll to Top