[java] Alternate IF statement

Started by
7 comments, last by Jonbca 21 years, 6 months ago
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.
Advertisement
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]
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
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 ==

???
= is the assignment operator

== is the boolean operator
~~~~~Screaming Statue Software. | OpenGL FontLibWhy does Data talk to the computer? Surely he's Wi-Fi enabled... - phaseburn
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]
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
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.
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**?
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.
"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.
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; 

This topic is closed to new replies.

Advertisement