While - Do While - For Loop Statements In Java


While statement use for iteration until provided expression is true.
While loop contains expression , check for expression is true or false, if expression is true then Code executed inside while loop until expression will not false.

WHILE FOR LOOP STATEMENTS :

 

SYNTAX:

while (booleanExpression) {
      // You Code Here....
  }

 

Example1 :

Below example showing Very basic example code for while loop.

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    int a = 1;
	    
	    // each iteration while check (a < 3)
	    // if find out (a < 3) is true execute inner code
	    while (a < 3) {
	    	
	      System.out.println("a = "+a);
	      
	      a++;  // In each iteration increase a variable value to 1
	      
	    }
	    
	  }   // Main close
	
 }

 

OUTPUT:

  a = 1
  a = 2

 

Example2 :

Below example is an infinite loop

 public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    while (true) {
	    	
	       System.out.println(" infinite loop ");
	      
	     }
	    
	  }   // Main close
	
 }

 

 

Example3 :

We can use break statement to break while loop iteration, After break statement execution will out from while loop

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    int a = 1;
	    
	    while (a < 10) {
	    	
	      System.out.println("a = "+a);
	      
	      if(a==6)
	        break;
	      
	      a++;  // In each iteration increase a variable value to 1
	      
	    }
	    
	  }   // Main close
	
 }

 

OUTPUT:

a = 1
a = 2
a = 3
a = 4
a = 5
a = 6

 

Example4 :

Below example is calculating sum of 10 numbers

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    int a   = 1;
	    int NumberSum = 0;
	    while (a <= 10) {
	    
	      // Add current a variable value to NumberSum	
	      NumberSum = NumberSum + a;
	       
	      a++;  // In each iteration increase a variable value to 1
	      
	    }
	    
	    System.out.println("Sum of numbers  = "+NumberSum); 
	    
	  }   // Main close
	
 }

 

OUTPUT:

Sum of numbers  = 55

 

DO WHILE LOOP: :

 

Do While statement also use for iteration until provided expression is true.
Do While loop contains expression , Do While loop firstly runs code once then
check for expression is true or false, if expression is true
then Code executed again until expression will not false.

SYNTAX:


    do{
        // You Code Here....
    }while (booleanExpression);
    

 

Example 5 :

Below code show do-while execute at least once.

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
		int x = 1;
		
	    do {
	    	
	          System.out.println("x = "+x);
	        
	          x++;
	        
	      } while (x > 2);  // expression false
	    
	    
	  }   // Main close
	
 }

 

OUTPUT:

   x = 1

 

Example 6 :

Below example is calculating sum of 10 numbers using do-while loop


  public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    int a   = 1;
	    int NumberSum = 0;
	    do {
	    
	      // Add current a variable value to NumberSum	
	      NumberSum = NumberSum + a;
	       
	      a++;  // In each iteration increase a variable value to 1
	      
	    }while (a <= 10);
	    
	    System.out.println("Sum of numbers  = "+NumberSum); 
	    
	  }   // Main close
 }
 

 

OUTPUT:

Sum of numbers  = 55

 

FOR LOOP :

 

For loop statement use for iteration until provided expression is true.
For loop contains expression , check for expression is true or false, if expression is true
then Code executed inside for loop until expression will not false.

SYNTAX:


    for ( initialize variable ; booleanExpression ; update initialize variable) {
	 
	      // You Code Here....
	       * 
	  }
	  
	initialize variable        :  initilize one or many variables. Ex. int i=0 
	booleanExpression          :  i < 5
	update initialize variable : update one or many variables. Ex. i++
    

 

See Below for loop examples

 

Example 6 :

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    for (int a = 0; a < 5; a++) {
	    	
	        System.out.println( "a = " + a);
	        
	    }
	  }   // Main close
 }

 

OUTPUT:

    a = 0
    a = 1
    a = 2
    a = 3
    a = 4

 

Example 7 :

Taking example for use of || operator.

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
		int a = 0;
		
	    for ( ; a < 5; a++) {
	    	
	        System.out.println( "a = " + a);
	        
	    }
	  }   // Main close
 }

 

OUTPUT:

    a = 0
    a = 1
    a = 2
    a = 3
    a = 4

 

 

Example 8 :

 public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
		int a = 0;
		
	    for ( ; a < 5 ; ) {
	    	
	        System.out.println( "a = " + a);
	        
	        a++;
	        
	    }
	  }   // Main close
}
 

 

OUTPUT:

    a = 0
    a = 1
    a = 2
    a = 3
    a = 4

 

Example 9 :

  public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
		int a = 0;
		
	    for ( ;  ; ) {
	    	
	        System.out.println( "a = " + a);
	        
	        if(a > 4)
	        	break;
	        
	        a++;
	        
	    }
	  }   // Main close
}

 

OUTPUT:

    a = 0
    a = 1
    a = 2
    a = 3
    a = 4   

 

Example 10 :

Below example is calculating sum of 10 numbers

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    int NumberSum = 0;
	    
	    for ( int a=1 ; a <= 10 ; a++) {
	    
	      // Add current a variable value to NumberSum	
	      NumberSum = NumberSum + a;
	       
	      
	    }
	    
	    System.out.println("Sum of numbers  = "+NumberSum); 
	    
	  }   // Main close
	
}

   

 

OUTPUT:


Sum of numbers  = 55
   

 

Example 11 :

Below example is infinite loop.

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
	    for (  ; ; ) {
	    
	    	System.out.println("Infinite Loop");
	       
	      
	    }
	    
	 }   // Main close
	
}

 

Example 12 :

Below example is showing how to take multiple initialization and updation in for loop

public class While_For_Loop_Statement {
	
	public static void main(String[] args) {
		
		for (int x = 0, y = 4; x < 5; x++, y--){
		      System.out.println("i = " + x + " , j= " + y);
		  }
	    
	 }   // Main close
	
}

 

OUTPUT:


i = 0 , j= 4
i = 1 , j= 3
i = 2 , j= 2
i = 3 , j= 1
i = 4 , j= 0
   

 

 

Share this: