Skip to content

Instantly share code, notes, and snippets.

@JakSla
Created April 16, 2021 19:12
Show Gist options
  • Select an option

  • Save JakSla/f128722ded5140748d3c7cc4c192eafb to your computer and use it in GitHub Desktop.

Select an option

Save JakSla/f128722ded5140748d3c7cc4c192eafb to your computer and use it in GitHub Desktop.
While vs do-while
//1
int number = 0;
while (number != 5) {
number++
}
//2
int anotherNumber = 0;
do {
number++;
} while (number !=5)
System.out.println(number == anotherNumber); //true
// 1 and 2 are equal at the end and in this case both loops have exactly the same behaviour
//1
int number = 5;
while (number != 5) {
number++
}
//2
int anotherNumber = 5;
do {
number++;
} while (number !=5)
System.out.println(number == anotherNumber); //false
// 1 and 2 are not equal, because while loop first checks the condition (number != 5) and
// do-while loop will always execute code at least once and then check the condition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment