This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Allowing if statement to have two options?

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to allow an if statement to have two options to move forward. Something like this.

 
Jan Engstrom
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The symbol for OR is ||
AND &&
NOT !

 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you use those operators, have a look at their precedence table.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Before you use those operators, have a look at their precedence table.


((Or use parentheses. ))

;-)
 
Winston Gutkowski
Bartender
Pie
Posts: 10217
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Engstrom wrote:The symbol for OR is ||
AND &&
NOT !

Sounds like you're a bit miffed (entirely Java's fault of course).

Winston
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you to everyone, forgot about logic operators for a second.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another little problem: the Java tutorials call && || logical and/or in one place and conditional and/or somewhere else. I think the Java Language Specification, which is definitive, calls them conditional.
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I am having a problem implementing the or in my program. Here is the bit of code that I an wishing to add an or to.




I would like it to operate like this. Using || throws an error

"The operator || is undefined for the argument type(s) java.lang.String, java.lang.String"

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ||, &&, and ! operators can only be used with boolean operands. If you write A || B then both A and B must be boolean expressions.

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:The ||, &&, and ! operators can only be used with boolean operands. If you write A || B then both A and B must be boolean expressions.



Well that got it working, and I understand that boolean operators are either True/False, but I want to make sure I understand this. The true and false in this bit of code is it testing whether userMove equals paper or 1 and it will return true/false or basically yes or no. Am I correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Corkrend wrote:
Well that got it working, and I understand that boolean operators are either True/False, but I want to make sure I understand this. The true and false in this bit of code is it testing whether userMove equals paper or 1 and it will return true/false or basically yes or no. Am I correct?


Given this:



This part of the expression:


evaluates to true if userMove is equal to "PAPER", OR is equal to "1"


"PAPER" : true
"paper" : false
"Paper" : false
"1" : true
"2" : false
"doggy": false

and so on.

If aiMove is equal to "Rock" , AND the above also evaluates to true, then the overall if condition evaluates to true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic