• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jeffige

count++ & infinite loop

14 posts in this topic

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

[source lang="java"]public class forDemo {

// Demonstrates the for loop


public 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 loop


public 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.
0

Share this post


Link to post
Share on other sites
To be honest, I'm not sure why [b]count = count++;[/b] 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
0

Share this post


Link to post
Share on other sites
When I write:
[source lang="java"]for (count = 0; count < 5; count++);[/source]
it gives me: [size="2"]This is count: 5[/size]

[size="2"]From what the author says it's suppose to display the exact loop as the first example:[/size]

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

thanks again.
0

Share this post


Link to post
Share on other sites
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).
0

Share this post


Link to post
Share on other sites
Um... nevermind. I guess this is why you should check the code first!

[b][color="#006699"]for[/color][/b] [color="#000000"](count = [/color][color="#009900"]0[/color][color="#000000"]; count < [/color][color="#009900"]5[/color][color="#000000"]; count = count + [/color][color="#009900"]1[/color][color="#000000"]); <<<<<semicolon - that's why it didn't work. sorry guys.[/color]

[quote name='jefferytitan' timestamp='1345091194' post='4970050']
It's just [i]count++[/i], not [i]count=count++[/i]. 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 [i]count++[/i] is count, whereas the return value of [i]++count[/i] 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 this
int a = count++; // a is 2, count is 3 after this
int b = ++count; // b is 4, count is 4 after this
[/source]
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1345092384' post='4970057']
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).
[/quote]

Yeah I just got that as you posted. Thanks Ruler
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
The result of the expression 'count++' is the value of count [i]before[/i] 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
0

Share this post


Link to post
Share on other sites
[quote name='eppo' timestamp='1345107051' post='4970096']
The result of the expression 'count++' is the value of count [i]before[/i] 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.
[/quote]

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

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites
[quote name='dimitri.adamou' timestamp='1345117054' post='4970116']
no its not. True, you assigning it 0 but then you increment it by 1.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
[quote name='dimitri.adamou' timestamp='1345117054' post='4970116']
no its not. True, you assigning it 0 but then you increment it by 1.
[/quote]
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 [i]before[/i] assignment occurs, not after.
1

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1345119916' post='4970131']
[quote name='dimitri.adamou' timestamp='1345117054' post='4970116']
no its not. True, you assigning it 0 but then you increment it by 1.
[/quote]
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 [i]before[/i] assignment occurs, not after.
[/quote]

thank you that makes more sense.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0