This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!