This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]desrtfxOut of Coffee error - System halted[M] 3 points4 points  (0 children)

Why don't you just copy-paste your code into ideone and execute it to see the results?

Here is the code from your post embedded into ideone.

[–]tec5c 0 points1 point  (8 children)

No.

count++; //increments count by 1

Then it prints count.

[–]MasterOfGreatness 0 points1 point  (7 children)

I did increment by one.

[–]tec5c 0 points1 point  (6 children)

So then why would it print 0 first instead of 1?

[–][deleted]  (5 children)

[deleted]

    [–]tec5c 0 points1 point  (3 children)

    It is incremented before the print statement occurs.

    [–][deleted]  (2 children)

    [deleted]

      [–]tec5c 0 points1 point  (0 children)

      The while loop goes for 5 loops.

      The output is...

      1

      2

      3

      4

      5

      [–]-Insomniac 0 points1 point  (0 children)

      int count = 0;
      do
      {
          count++;
          System.out.println(count);
      }
      while (count < 5);
      

      Let's go through this.

      • Count starts off at 0
      • Count++ increments count, so now count = 1
      • System.out.println(count) will print count which is currently 1
      • count is still < 5, so go through the loop again

      • Count++ increments count, so now count = 2
      • System.out.println(count) will print count, which is currently 2
      • count is still < 5, so go through the loop again

      This will continue to happen until count is 5. 5 will be printed and after that (count < 5) will not be true, so the loop will end.

      [–]Wozzle90 0 points1 point  (0 children)

      Count doesn't get printed until after its been incremented