#### Archived

This topic is now archived and is closed to further replies.

# [java] Alternate IF statement

## Recommended Posts

Jonbca    127
ok, I''m making a database for my programming class and I want to be able to check a variable using the ? method in c++ but I don''t know how to do it. Actually, I''m not even sure you''re aloud to. The syntex in c++ is:

arg1 = arg2 ? returnSomeThing: orSomeThingElse

I think I''ve seen it in a java program somewhere, but I''m not sure. ANy help would be appreciated.

##### Share on other sites
WayfarerX    130
Works just like C++.
Object value = getSomeValue();String result = value == null ? "value-is-null" : "value-is-not-null";

[edited by - wayfarerx on October 1, 2002 4:46:10 PM]

##### Share on other sites
Jonbca    127
I tried to do something pretty similar to that. I''m going

state[4]==1 ? search.Show(true):search.Show(false);

but I''m getting an invalid assignment error on the ==

???

##### Share on other sites
dede    132
= is the assignment operator

== is the boolean operator

##### Share on other sites
WayfarerX    130
From http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293
quote:
Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear (§14.8).

Is that what you're doing? Also, wouldn't this be a little more concise in your scenerio?
search.Show(state[4] == 1);

[edited by - wayfarerx on October 1, 2002 5:04:06 PM]

##### Share on other sites
Jonbca    127
thanks a lot. Both of my methods were a void return so that way of doing it wouldn''t work. Thanks for the

search.Show(state[4]==1)

idea. Wow, can you say blond moment, gotta love missing something that easy.

##### Share on other sites
Guest Anonymous Poster
I hate this syntax....it''s obfuscated and JAVA would be a better (cleaner) language without it. I don''t find much other cryptic stuff in JAVA, so why this sh**?

##### Share on other sites
Viro    122
quote:
Original post by Anonymous Poster
I hate this syntax....it''s obfuscated and JAVA would be a better (cleaner) language without it. I don''t find much other cryptic stuff in JAVA, so why this sh**?

What so obfuscated about that? Besides, if you don''t like it, you could do something else.
if(state[4]==1)	search.show(true);else	search.show(false);

I suppose its a matter of preference. I''d still prefer the original syntax because it is more concise.

int x = (y % 2 == 0) ? 5 : 10;// Is nicer than:int x = 0;if (y % 2 == 0)  x = 5;else  x = 10;