while(false){

System.out.println("While ran");

}

do{

System.out.rpintln("Do while ran");

} while(false);

The output of the code above is "Do while ran" only. This is a classic example of the difference between do/while and while. Both will only run if the condition is true. However do/while will run the code block and then test the condition. A while loop will test the condition and then run the code block.

Do/while is great when you always want to run some code AT LEAST once. Other than that stick to a while loop.

next converting itteration >>

Note - In psuedocode there is something known as a repeat/until. In Java this is the do/while loop but in other languages it is called repeat/until. They do work slightly differently in the fact that repeat/until will only loop if the condition evaluates to false. Ignore the rest of this page if you are only interested in Java.

  1. repeat
  2. print("repeat ran");
  3. until false

The above code (written in psudeocode) will loop forever!

There are two major differences from repeat/until and while / for -

  1. A=0
  2. WHILE A<=10
  3. PRINT A
  4. A = A + 1
  5. END WHILE

Converting this into a REPEAT/UNTIL -

  1. A=0
  2. REPEAT
  3. PRINT A
  4. A = A + 1
  5. UNTIL A>10

The condition appears at the bottom to signify that the loop will always run once. Also notice that the condition has changed from A<=10 to A>10 . This is because the loop will only run if the condition is false (so we effectively inverse the condition).

next converting itteration >>

 

 

 
Basics
Menu
Search