This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternate way of writing an if statement...  RSS feed

 
George Larry
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I remember seeing someone write an if-then statement in one quick line... like:
if (blah == true) superBlah = y:n
Where... if the statement is true then superBlah ='s y... if not it's set to n.
Could someone please verify this and correct my syntax?
Thank you kindly,
-GL
 
George Larry
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also... can I put an if inside an out.println()?
Like this:

It would be nice if something like that worked... If anyone has done anything like that- please let me know how to make it work.
Thanks,
-GL
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although more of a beginner Java question than a JSP one, what you are thinking of is the conditional expression:

the value of the expression evaluates to the if-true clause if expression is true, if-false otherwise.
Note, it is considered very bad practice to use this as an if-statement replacement; rather, use it when you really want a conditional expression not a conditional statement.
A common example of its usage:
System.out.println( "Found " + count + " item" + ((n == 1) ? "" : "s") );
hth,
bear
[ September 17, 2002: Message edited by: Bear Bibeault ]
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think that's what you're getting at.
 
Harpartap Singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the first part:

Conditional operator (? is another form of if/else. It takes three operands. The first operand is a boolean expression, if first ezuates to true the second is used, else the third is used.
Now this could be used as following for your example:

----------
OUTPUT:
----------
SuperBlah = y...So there.
Regards,
/hs
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!