aspose file tools*
The moose likes Beginning Java and the fly likes Allowing if statement to have two options? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Allowing if statement to have two options?" Watch "Allowing if statement to have two options?" New topic
Author

Allowing if statement to have two options?

John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Is there a way to allow an if statement to have two options to move forward. Something like this.

Jan Engstrom
Greenhorn

Joined: Mar 29, 2013
Posts: 5
The symbol for OR is ||
AND &&
NOT !

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Before you use those operators, have a look at their precedence table.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Campbell Ritchie wrote:Before you use those operators, have a look at their precedence table.


((Or use parentheses. ))

;-)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8010
    
  22

Jan Engstrom wrote:The symbol for OR is ||
AND &&
NOT !

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

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
John Corkrend
Ranch Hand

Joined: Oct 27, 2012
Posts: 51

Thank you to everyone, forgot about logic operators for a second.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 27, 2012
Posts: 51

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 27, 2012
Posts: 51

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allowing if statement to have two options?