• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about boolean logic and if statements

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic