Created
April 16, 2021 19:12
-
-
Save JakSla/f128722ded5140748d3c7cc4c192eafb to your computer and use it in GitHub Desktop.
While vs do-while
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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