multiple "if" statements in java

mdchesne

Banned
Feb 27, 2005
2,810
1
0
if I want to say

if (so and so) { do this }
if (so and so, not the same as above) {do this, same as above }
if (.....) { do this, same as above }

can I write it

if (so and so),(diddly do),(poopy head) {do this}

where any one of the "ifs" can happen and it will "do this".
commas between if statements or commas between if statements while inside the same parenthesis? I forgot over summer

trying to save space and time on code.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
edit: nevermind, you want to do the same thing.

Use ors then, or the || symbol

if ( (so_and_so) || (something_else) || etc... ) { do this }
 

brockj

Golden Member
Jul 6, 2005
1,135
0
0
You can do that by using the or operator. I think the OR operator is 2 pipes ||

If x=1 || x > 7
Do something
Else
Do something else
end if

Something along those lines...I am not 100% sure that it is 2 pipes...it has been a while since I have done some Java programming.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
i think it is the || operator. i knew something went between them, just forgot what. I'll try

if (this)||(this)||(or this) {then do this!}
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
hmmm... mmmm, what about if all of them need to be true?

if (this)^(this)^(and this) { do this } ??
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Originally posted by: mdchesne
hmmm... mmmm, what about if all of them need to be true?

if (this)^(this)^(and this) { do this } ??

Read my editted post above ;)

It's the '&&' symbol
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
omg, you're right. i will never go 6 months without at least looking at java again. i feel so stupid now. lol. thanks
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
For this program that is 200-300 lines long WITH comments, I would hardly do things like that "to save space and time". You are better off laying things out better so that you understand it well, rather than cram it in and have it look like sh*t for an introductory data structs class.
 

znaps

Senior member
Jan 15, 2004
414
0
0
Originally posted by: duragezic
For this program that is 200-300 lines long WITH comments, I would hardly do things like that "to save space and time". You are better off laying things out better so that you understand it well, rather than cram it in and have it look like sh*t for an introductory data structs class.


Using || or && IS the correct way to make the code more understandable (and performant).
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Oh I understood his question wrong. Of course... not using those can make it look like crap.