Java – While Statements

What is the difference between while and do while loop?

Do while

The do while loop will check the condition at the end of the loop. This way, the code inside the loop will be executed atleast once.

Example

class Workout{
    int reps = 10;    int sets = 0;    int total_reps = 0;    Workout(){        
do {            
     total_reps = total_reps + reps;           
     sets++;            
     System.out.println("Total reps:"  + total_reps);        
     } while (sets < 4);        
     return ;
    }
        public static void main(String[] args) {            
            Workout swole = new Workout();    
          }
}


Control-Flow

While Loop

The while loop checks a given condition. If the statement is true, the loop will be executed, else it will stop looping and move on to the next piece of code.

Difference between while and do while loop:

The while loop starts executing once the condition is checked and the do while will execute once (even if its false) and then check the condition.

while(condition = true){
Jake = smart;
}
view raw while.java hosted with ❤ by GitHub

Leave a comment

Design a site like this with WordPress.com
Get started