Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

boolean comparisons  RSS feed

 
kelly ball
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I tried a line of code to compare a boolean value

and the loops I was using for comparisons would not work. I later tried

and it worked
we use Bluej at college and I can't see why but is this in conflict in some way ?
would appreciate any comments
kelly
[ January 24, 2004: Message edited by: Marilyn de Queiroz ]
[ January 25, 2004: Message edited by: kelly ball ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to post a little bit more code here in order for us to help, cause i don't see any reason at all why it should not work.
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that you didn't type

if (x = true)

by mistake?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that matter, you could also just use
if (x)
As for the original question, see if you can replicate the problem, and post the exact code that demonstrates it. Otherwise I think Marilyn has the right idea about what happened.
 
kelly ball
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi - thanks for your help on this so far
a mis-type " = " for " == ", would be a very simple explanation, but I don't think I did
have reviewed an earlier example I was working on and this is a little more of the code to put it in context

here I was using " nameExists " and it is clearly " = = "
this line when changed to

and it worked [I still had a problem with the loops testing but at least I got the conditional 'if' statement working
regards kelly
thanks for your efforts, but I am still baffled
just reviewing this post - the line about 3 above the 'if test' - is this causing my problem? kelly
[ January 25, 2004: Message edited by: kelly ball ]
 
Ram Mohan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm also baffled with regard to this mystery....One question though, why do you have a break statement in your if-condition??? Adds complexity to the code!
since if(nameExists!=false) works, its value is being changed to something else...(probably null)
[ January 25, 2004: Message edited by: Ram Mohan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change "nameExists != false" back to "nameExists == true" does the problem recur? If not, perhaps something else was going on, like maybe the program wasn't compiled correctly and you were executing an old class file which did not correspond to the latest .java file you were looking at?
As for the break, well this is exactly the sort of situation where I probably would use a break as shown - but with a better comment. I can't tell what "stop here in branch" means. How about "// found name, so exit while loop"? But for the question about a break in a while loop, different people have different opinions on this. I'd say if you do it, it's important that the surrounding code be as clear as possible, since some people who read your code will automatically regard the break as "complex".
Actually the biggest problem I see with this code is that the indentation is horrible. Need to be able to see the loop structure clearly, and there is never any reason for tolerating bad indentation in code. Making your program readable will make all other taks easier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!