This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can you use && two times in one if statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you use && two times in one if statement?" Watch "Can you use && two times in one if statement?" New topic
Author

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

Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
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

Joined: Aug 14, 2001
Posts: 342
Yes, it is OK


SCJP 1.4, www.gsi3d.org.uk
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can you use && two times in one if statement?
 
Similar Threads
how to save values at a perticular index of the List?
Deciding on a data structure to optimize performance
Why static methods can't be defined inside Inner classes
B&S: Techinical Questions
Determining required fields flexibly