سؤال

Why does the following code execute six times? Please help me to understand how this works, as I've tried to get it into my head without success.

I thought it would first execute the code once, then increase count to 1, execute it a second time, increase count to 2, execute it a third time, increase count to 3, execute it a fourth time, increase count to 4, execute it a fifth time, increase count to 5, and then stop. Which means it will have executed the loop five times (for the first time, then for when count is 1, 2, 3, 4).

int count = 0;

    do {

        System.out.println("Welcome to Java!");

    } while (count++ < 5);
هل كانت مفيدة؟

المحلول

Have you tried running this code?

int count = 0;

do {

    System.out.println("Welcome to Java! " + count);

} while (count++ < 5);

output:

Welcome to Java! 0
Welcome to Java! 1
Welcome to Java! 2
Welcome to Java! 3
Welcome to Java! 4
Welcome to Java! 5

This should help you understand what is happening. Has others have said your confusion is most likely in how the post increment operator works.

To help you understand pre and post increment operators lets run another code sample

int a = 0;
int b = 0;
System.out.println("pre increment "+ ++a);
System.out.println("post increment "+ b++);

output:

pre increment 1
post increment 0

Summarizing: with post increment the expression is evaluated before the variable is incremented, with pre increment the expression is evaluated after the variable is incremented.

نصائح أخرى

Its postfix operator so first evalution of whole expression then increment

Control will flow like this

0
Welcome to Java!
//condition check : 0 then 1
Welcome to Java!
//condition check : 1 then 2
Welcome to Java!
//condition check : 2 then 3
Welcome to Java!
//condition check : 3 then 4
Welcome to Java!
//condition check : 4 then 5
Welcome to Java!
//condition check : 5 then 6

That's because you're using post-increment. The while condition is first evaluated (with the count value from BEFORE the increment) and then count is incremented.

Try ++count (which first increments and then returns value).

edit:

Note that although using it in

for(int i = 0; i < n; i++) {}

is ok (it will usually get optimized, etc.),

for(int i = 0; i < n; ++i) {}

is a little bit better from the semantic point of view IMO.

It gets even more complicated in languages with operator overloading, where i++ can have different sideffects than ++i.

count++; // <-- would execute the above code 6 times

is post increment and

++count; // <-- would execute the above code 5 times 

is pre increment

Consider:

while (count++ < 5) System.out.println(count); // prints 1 2 3 4 5
while (++count < 5) System.out.println(count); // prints 1 2 3 4

So your do...while executes first without a comparison (because of the do) then runs the comparison.

If it's pre-increment it could be re written like this:

int count = 0;
do {
    // print
    count = count + 1;
} while (count < 5)

If it's post-increment it could be re written like this:

int count = 0;
while (count < 5) {
     // print statement
     count = count + 1;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top