• 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

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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is OK
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can use as many && operators as you need, just make proper logic and it should work!
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic