For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > Some short code problem.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Some short code problem.
Zalek Bloom

2004-03-27, 12:30 am

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

2004-03-27, 12:30 am

> 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);

Roedy Green

2004-03-27, 12:30 am

On Fri, 26 Mar 2004 00:47:03 GMT, Zalek Bloom <ZalekBloom@hotmail.com>
wrote or quoted :

>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

2004-03-27, 12:30 am

In article <t5v660111tsmens30p2j5ks8oaun9ak1ra@4ax.com>,
Zalek Bloom <ZalekBloom@hotmail.com> wrote:

> 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
Zalek Bloom

2004-03-27, 12:30 am

On Fri, 26 Mar 2004 03:04:09 +0100, "Agata Staniak"
<agaace@usenet.vitryna.pl> wrote:

>
>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);


Thanks Agata,

Dziekuje za pomoc,

Zalek
Alex Hunsley

2004-03-27, 12:30 am

Zalek Bloom wrote:
> 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?


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

Bryce (Work)

2004-03-27, 12:30 am

On Fri, 26 Mar 2004 00:47:03 GMT, Zalek Bloom <ZalekBloom@hotmail.com>
wrote:

>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?


Because m < 2 is tested before it is incremented. ++m will increment
prior to testing.

--
now with more cowbell
Grant Wagner

2004-03-29, 2:41 pm

Alex Hunsley wrote:

> Zalek Bloom wrote:
>
> 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


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>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com