Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

syntax question?  RSS feed

 
cardwell cupp
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reviewing some code lately, and have come across a line that uses a ? and a : I am assuming that this is some kind of shortcut for an if statment. Is that correct. Could anyone give me an explanation for those 2 symbols? How do you use them.
Thanks
Cardwell
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cardwell,
Yes you are right. It is called conditional statement. An example:
System.out.println( "x = " + (x == null) ? null : x.intValue() );
The above example is equal to the following:
if( x == null )
{
System.out.println( "x = " + null );
}
else
{
System.out.println( "x = " + x.intValue() );
}
The '?' character means choose one of the following. The ':' character separates the two choices. The first choice is the 'true' value while the second one is the 'false' value.
Regards,
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!