friends! I can not understand why the output from 1 to 10, and not to 11, if at first it is satisfied, and then the condition is checked

class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } } 
  • four
    Take a piece of paper and a pencil, and go through the code step by step, writing down the values ​​of the variables. - Igor

2 answers 2

Go to the last lap:

  1. We print "Count is: 10".
  2. We add 1 to 10, we get 11.
  3. Compare 11 with 11. 11 not less than 11. The condition is not satisfied -> We leave the cycle.

If you write count <= 11 , then count <= 11 will be printed.

    When count = 10 is incremented and the count variable is equal to 11, in the while (count <11) condition we stop the cycle if count <11.

    • 2
      in the condition while (count <11) we stop the cycle if the count <11 is not, if count <11, then the cycle just continues. - Enikeyschik
    • Yeah, right. The condition continues until the count <11, as soon as the count becomes 11, we stop the loop. - Ivan Chaykin pm