Try to avoid interruptions, there is always another way to write your own loop so that it does not need it, which is much more “beautiful” and easier to understand if someone else needs to change your code. In your example, a while loop is not needed, but to show you how this is possible:
while(x > 0) { for(int i = 5; i > 0 && x!=0; i++) { x = x-2; } }
If x is 0, the for loop will be left. Then your condition will be checked: x is less than 0 (it is zero), so your while loop will also stop executing.
Jonathan kortleven
source share