I have a problem with explanation of the following question:
What will be the result of the following code:
int m = 0;
while( m++ < 2 )
System.out.println( m );
I tested this code and the answer is:
1
2
My question: how come the number 2 is printed?
I noticed that if I changed the code to:
int m = 0;
while( ++m < 2 )
System.out.println(m );
Only number 1 is printed (this I understand why).
Any ideas why?
Thanks,
Zalek
Agata Staniak - 26 Mar 2004 03:04 GMT
> What will be the result of the following code:
>
> int m = 0;
> while( m++ < 2 )
> System.out.println( m );
When the application enters the loop for the first time, m=0.
When m is compared with 2, m is still equal 0. Then, just after the
comparison,
the code m++ is executed, and after that, m =1. And this is printed out.
And again the application enters the loop. When m is compared with 2, m is
still 1.
So (m < 2) is true. Just after the comparison, m is increased, and now m=2.
This is printed out. Now, when the next comparison is executed, (m < 2) is
false, because m is now 2. So the loop stops.
Remember: (m++ < 2); is the same as you would write: (m < 2); m++;
And (++m < 2) means: m++ (or ++m); (m < 2);
Zalek Bloom - 26 Mar 2004 12:00 GMT
>> What will be the result of the following code:
>>
[quoted text clipped - 14 lines]
>Remember: (m++ < 2); is the same as you would write: (m < 2); m++;
>And (++m < 2) means: m++ (or ++m); (m < 2);
Thanks Agata,
Dziekuje za pomoc,
Zalek
Roedy Green - 26 Mar 2004 04:09 GMT
>int m = 0;
>while( m++ < 2 )
This is bastard code. This should be implemented with for loop. It
may be syntactically correct, but it stylistically wrong.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Haase - 26 Mar 2004 06:56 GMT
> Any ideas why?
m++ is the post-fix notation...it evaluates the rest of the expression
BEFORE it increments m
++m is the pre-fix notation, it increments m first and then evaluates
the rest of the expression with that new value of m
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Alex Hunsley - 26 Mar 2004 12:15 GMT
> I have a problem with explanation of the following question:
>
[quoted text clipped - 10 lines]
>
> My question: how come the number 2 is printed?
The other replies answer this masterfully...
just to add a comment: to keep code simple, and understandable, only
increment or decrement values on their own, and not as a larger
expression. i.e.:
// BAD
while( m++ < 2 )
System.out.println( m );
// GOOD
while (m < 2) {
m++;
System.out.println( m );
}
Both these snippets will do exactly the same thing, but the second is
easier to understand, especially at your level experience.
alex
Grant Wagner - 29 Mar 2004 19:43 GMT
> > I have a problem with explanation of the following question:
> >
[quoted text clipped - 32 lines]
>
> alex
Indeed, mixing conditions with expressions that can produce side-effects
is a Bad Thing(tm). Consider:
int displayCount = 0;
for (int i = 0; i < rows; i++) {
if (whatever[i] != somethingOrOther) {
continue;
}
if (displayCount++ > 0) {
System.out.println("---"); // row separator
}
System.out.println(whatever[i].toRowString());
}
Now they want even/odd rows in different colors:
int displayCount = 0;
for (int i = 0; i < rows; i++) {
if (whatever[i] != somethingOrOther) {
continue;
}
if (displayCount++ > 0) {
System.out.println("---");
}
if (displayCount % 2 == 0) {
whatever[i].setEvenRowColor();
} else {
whatever[i].setOddRowColor();
}
System.out.println(whatever[i].toRowString());
}
Hmmm, the row colors are reversed. Much better if it had been coded as:
int displayCount = 0;
for (int i = 0; i < rows; i++) {
if (whatever[i] != somethingOrOther) {
continue;
}
if (displayCount > 0) {
System.out.println("---");
}
if (displayCount % 2 == 0) {
whatever[i].setEvenRowColor();
} else {
whatever[i].setOddRowColor();
}
System.out.println(whatever[i].toRowString());
displayCount++;
}
Not only does it avoid any side-effects, but it (in my opinion) more
clearly communicates that "displayCount" is counting the number of rows
we've actually output, by moving it closer to the line that actually does
the outputting.
Some would include [while ((l = d.readLine()) != null)] in the list of
"Things Not To Do".
--
| Grant Wagner <gwagner@agricoreunited.com
Bryce (Work) - 26 Mar 2004 15:13 GMT
>int m = 0;
>while( m++ < 2 )
[quoted text clipped - 6 lines]
>
>My question: how come the number 2 is printed?
Because m < 2 is tested before it is incremented. ++m will increment
prior to testing.
--
now with more cowbell