Archived

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

Jonbca

[java] Alternate IF statement

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also, it makes code like this much nicer, and I would say safer because it helps prevent you forgetting to initialising a variable:


int x = (y % 2 == 0) ? 5 : 10;

// Is nicer than:

int x = 0;

if (y % 2 == 0)
x = 5;
else
x = 10;

Share this post


Link to post
Share on other sites