• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about boolean logic and if statements  RSS feed

 
Ken Whitestone
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?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • 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.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!