Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


count++ & infinite loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 15 August 2012 - 09:49 PM

Working through java, a beginners guide 5th ed one of the examples:

[source lang="java"]public class forDemo {// Demonstrates the for looppublic static void main(String[] args) { // TODO Auto-generated method stub int count; for (count = 0; count < 5; count = count + 1); System.out.println( "This is count: " + count); System.out.println( "Done!"); }}[/source]

The author goes on to say that the example is not good programming and that you will not see programmers use that example, you would probably se this instead: count++.

But when I run it with the latter:

[source lang="java"]public class forDemo {// Demonstrates the for looppublic static void main(String[] args) { // TODO Auto-generated method stub int count; for (count = 0; count < 5; count = count++); System.out.println( "This is count: " + count); System.out.println( "Done!"); }}[/source]

it runs "this is count: 0 infinitely

thanks for any help.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13880

Like
0Likes
Like

Posted 15 August 2012 - 10:22 PM

It's not `count = count++'. It's simply `count++'.

#3 jefferytitan   Crossbones+   -  Reputation: 2242

Like
3Likes
Like

Posted 15 August 2012 - 10:26 PM

It's just count++, not count=count++. It's not better programming per se, just shorter. I don't use it the way you have, but if I recall correctly the return value of count++ is count, whereas the return value of ++count is count+1.

It makes more sense as below:
[source lang="java"]int count = 0;count++; // count is 1 after this++count; // count is 2 after thisint a = count++; // a is 2, count is 3 after thisint b = ++count; // b is 4, count is 4 after this[/source]

Edited by jefferytitan, 15 August 2012 - 10:42 PM.


#4 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 15 August 2012 - 10:37 PM

To be honest, I'm not sure why count = count++; isn't working (even though you should only write it as count++). It looks like it could be language specific behaviour

What should happen is count is assigned the value of count, then it is incremented by 1.


count = count++ //1
count = count++ //2

I'm interested as to why this isn't true in java

#5 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 15 August 2012 - 10:40 PM

When I write:
[source lang="java"]for (count = 0; count < 5; count++);[/source]
it gives me: This is count: 5

From what the author says it's suppose to display the exact loop as the first example:

[source lang="java"] for (count = 0; count < 5; count = count +1);[/source]

thanks again.

#6 RulerOfNothing   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 15 August 2012 - 10:46 PM

Another thing, if you put a semicolon after a loop statement it turns the loop into a empty loop (since the semicolon by itself is simply the null statement). To fix this problem you need to get rid of the semicolon after the for statement (but only after the for statement).

#7 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 15 August 2012 - 10:51 PM

Um... nevermind. I guess this is why you should check the code first!

for (count = 0; count < 5; count = count + 1); <<<<<semicolon - that's why it didn't work. sorry guys.

It's just count++, not count=count++. It's not better programming per se, just shorter. I don't use it the way you have, but if I recall correctly the return value of count++ is count, whereas the return value of ++count is count+1.

It makes more sense as below:
[source lang="java"]int count = 0;count++; // count is 1 after this++count; // count is 2 after thisint a = count++; // a is 2, count is 3 after thisint b = ++count; // b is 4, count is 4 after this[/source]


So would that be more relevant, er... a better writing of that code? I just don't want to get into the habit of writing code people may frown upon.

#8 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 15 August 2012 - 10:52 PM

Another thing, if you put a semicolon after a loop statement it turns the loop into a empty loop (since the semicolon by itself is simply the null statement). To fix this problem you need to get rid of the semicolon after the for statement (but only after the for statement).


Yeah I just got that as you posted. Thanks Ruler

#9 jefferytitan   Crossbones+   -  Reputation: 2242

Like
0Likes
Like

Posted 15 August 2012 - 11:00 PM

The traditional way is as stated:
for (count = 0; count < 5; count++)

I was just pointing out that it won't make your code in any way faster or better, just more concise.

#10 eppo   Crossbones+   -  Reputation: 2608

Like
0Likes
Like

Posted 16 August 2012 - 02:50 AM

The result of the expression 'count++' is the value of count before incrementing it (0). So a repeated 'count = count++' is basically the same as saying 'count = 0'.
'count = ++count' would give the expected result, although there is no point in assigning count's value to itself.

Edited by eppo, 16 August 2012 - 02:53 AM.


#11 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 16 August 2012 - 05:37 AM

The result of the expression 'count++' is the value of count before incrementing it (0). So a repeated 'count = count++' is basically the same as saying 'count = 0'.
'count = ++count' would give the expected result, although there is no point in assigning count's value to itself.


no its not. True, you assigning it 0 but then you increment it by 1.

#12 obizues   Members   -  Reputation: 176

Like
0Likes
Like

Posted 16 August 2012 - 06:22 AM

The problem is as stated above, the semicolon. You need to take that out. It's also good practice to add brackets after the for statement even if there is only one statement. What your code is doing is looping your entire for, and then printing out the result.

For example:

For (i=0;i<5;i++)
{
System.out.print("test: " + i);
}


#13 Trienco   Crossbones+   -  Reputation: 2222

Like
0Likes
Like

Posted 16 August 2012 - 06:23 AM

no its not. True, you assigning it 0 but then you increment it by 1.


The typical implementation of a post increment would look like this:

tmp = count;
count = count + 1;
return tmp;

So no, you're not assigning first and then increment, you store the original value, then increment and the actual assignment is a completely different operator that happens last. In fact, one might argue that any compiler optimizing this code in a way that behaves the way you expect could be considered broken. You can't interrupt one operator (++) halfway through to squeeze in a different operator (=) and then go back to finish the first one.
f@dzhttp://festini.device-zero.de

#14 SiCrane   Moderators   -  Reputation: 9661

Like
1Likes
Like

Posted 16 August 2012 - 06:25 AM

no its not. True, you assigning it 0 but then you increment it by 1.

In Java, both sides of a non-short circuit binary operator are fully evaluated, including side effects, before the operator is applied. In this case it means that count is incremented before assignment occurs, not after.

#15 dimitri.adamou   Members   -  Reputation: 329

Like
0Likes
Like

Posted 16 August 2012 - 06:51 AM


no its not. True, you assigning it 0 but then you increment it by 1.

In Java, both sides of a non-short circuit binary operator are fully evaluated, including side effects, before the operator is applied. In this case it means that count is incremented before assignment occurs, not after.


thank you that makes more sense.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS