If Else Short form

In most language a short form of if else is available. It is a space saver as well as helps maintain clean code. The form is

if (condition=true) then return 1 else return 0. It can be written in different languages as follows

Bool Paid;
Paid = IsTheBillPaid(id) ? 1 : 0;

The actual condition here after the equal sign. The fist statement can be a function or logical test. The function must return a logical true or false value in order to work here. If the value is true the value after question mark (?) mark is returned. Otherwise the value after colon (:).

string result;
if (number1 > number2>
result = “greater”;

In short hand form, it becomes

string result;
result = number1 > number2 ? “greater” : “smaller”;

As you can see the code with shorthand form is much cleaner and clear. This was brief demonstration of “if else short form”.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s