• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you use && two times in one if statement?

 
Ling Chung
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it ok to use && two times in one if statement?
Like this :

As you can see i use && two times in one if statement is this ok?
Thanks for the help!!
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is OK
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use as many && and || as you want. i've seen as many as 27 conditions all ANDed and ORed together.
Note that this is probably not good programming technique (the 27), but a few are usually ok
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
you can use as many && and || as you want. i've seen as many as 27 conditions all ANDed and ORed together.
Note that this is probably not good programming technique (the 27), but a few are usually ok


What are you saying Fred... 27?!?! What a bowl of spagheti... but am puzzled with something else about if/else statements while doing my homework:
When nesting if-else statements, it happens that compiler throws a message "else without if" although in the source code "if" stands where it should be... parentheses are fine too...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You likely have some mismatched and/or missing brackets. If you were to post your problemmatic code, folks could help nudge you more in the right direction.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have any number of && and || operators, but watch you when you mix them. I like to go overboard with parens and be very explicit about how I'm combining things. Humans understand things like "If it's Monday and it's raining or if it's Tuesday and it's snowing ..." and programming languages have rules for interpreting such things, but remember to code for the humans first, the computer second. More parens make it explicit what you mean:

If you find this getting mildly complex (stop far short of 27!) see if you can't break it up. Find names for the combinations and set temporary variables or call tiny methods that do some of the comparisons.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic