• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite loop problem.

 
Riley Redd
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pastebin link to my code

Can someone please tell me:
Why won't the while loop break, even though the expression is false?



(but in order to reach that I know I have set playerTurn = false: )

 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't access your link. many folks work behind firewalls that block such sites.

Can you post your code here so everyone can see it?
 
Riley Redd
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, whenever I put code in on this site people get upset about code length and what not and then I spend 20 minutes reformatting my code, and even if it is 1 character above 80 after that, people still get upset....so I've been trying to find other solutions to posting directly here ^.^'
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riley Redd wrote:Why won't the while loop break, even though the expression is false?



(but in order to reach that I know I have set playerTurn = false: )


But the expression isn't false. The value of an assignment expression (the one denoted by "=") is the value of its right-hand side, which is also assigned to the variable on the left-hand side. And that value is clearly "true".

Don't confuse assignment (=) with equality comparison (==).
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riley Redd wrote:hmm, whenever I put code in on this site people get upset about code length and what not and then I spend 20 minutes reformatting my code, and even if it is 1 character above 80 after that, people still get upset....so I've been trying to find other solutions to posting directly here ^.^'

I'm afraid it's just one of the things you have to do.

First: it was me who complained; and that was more because after I'd just spent 5 minutes breaking up your original post and warning you about it, I came back to find that you'd edited it again, making the lines even longer than they were to start with.

Second: Long lines are simply bad practice, just as not using whitespace or using overly short or long names are. You want people to be able to read your code (and your threads) easily, don't you? That fact is that 1 or 2 characters over 80 isn't likely to make much difference, but 20 will.

Third: There are two problems in your
while (playerTurn = true);
line; either of which might be the issue.

The first, as Paul pointed out, is that you're using the assignment operator. The correct way to write an expression involving a boolean is:
while (playerTurn)

The second is that terminating ';' - It's a null statement, so the loop as written won't do anything, which is yet another reason why it might well run forever, even if you got the expression right.

HIH

Winston
 
Riley Redd
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*facepalm*
Why is it always the simple things that make mistakes?
thank you!


(as a side note so I don't make a pointless thread.)
Is using pastebin here bad?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch! Two gotchas in one line of code! I didn't even see that second one.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riley Redd wrote:Is using pastebin here bad?


It's not "bad" as in "evil bad". But when I see "Link to my code" I suspect a massive code dump, so I don't bother to follow such links. And as fred pointed out, sometimes people can't follow those links. (You might be surprised how much time people spend following the Ranch while they are at work.) Code posted right here is better.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riley Redd wrote:(as a side note so I don't make a pointless thread.)
Is using pastebin here bad?

I believe Fred already answered that one.

But, quite apart from the basic issue of clicking on "iffy" links, the fact is that folks here don't really want to go through your entire program; they want to see a digest of your problem in condensed form. That in itself requires a fair bit of work on your part, so breaking up excessively long lines is likely to only be a minor irritant.

My advice: Read the SSCCE page. It describes the process extremely well. You might also want to look at HowToAskQuestionsOnJavaRanch (←click).

Winston

PS: I notice that the SSCCE link suggests keeping lines to under 62 characters so, in fact, we're pretty lax here.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean to pile on here. But think about it this way...

When you are asking people to give up some of their time to help you, a complete stranger, you should want to make it as EASY as possible for them to do so. You want them to think you really care about this question, and are willing do some work yourself.

This may not be fair or rational. But every single thread I read, these things go through my head: 1) How much effort do I have to put in to help, and 2) how much effort have they put in to this themselves. if 1 < 2, odds are good I will help (if i can). if 1 >= 2, odds are I won't.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:if 1 < 2, odds are good I will help (if i can). if 1 >= 2, odds are I won't.

So...you help everyone then? What a dashed decent fellow you are, to be sure.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic