• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Question about boolean logic and if statements  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program written two different ways, and it seems to me that the final output should be exactly the same. I was wondering if someone could help me understand why the two versions aren't "logically" the same.

Version 1:


Version 2:


Version 1 returns true regardless of what values are stored in the "nums[]" array. Version 2, however, works exactly as intended. The two versions seem to evaluate the same way, but this is clearly not the case. Why is that?
 
Ranch Hand
Posts: 2410
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your programs and got false on both
 
Ken Whitestone
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Keith Lynn wrote:I ran your programs and got false on both


Grrr!! I found the problem...

In my original copy of Version 1, I missed the fact that I had accidentally added a semicolon at the end of the if statement:



I've spent hours breaking down every aspect of the code and trying to understand why it wasn't working as it was supposed to. I've never run into that problem before, but it makes perfect sense why it wouldn't work the way I wanted it to.

Thank you for your help. It helped me to identify the problem and probably saved me from more hours spent looking in the wrong direction.


Sorry if I wasted your time.
 
Rancher
Posts: 1020
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've spent hours breaking down every aspect of the code and trying to understand why it wasn't working as it was supposed to.



1)
It is strange that you posted here a different version.

2)
There is a saying that if all else fails when trying to tame a piece of hardware or software, one should read the documentation.
Well, if all else fails, a session with a debugger can be useful in this situtation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!