Conditional Operators

In C programming language conditional operators are also called Ternary operators. These operators are used instead of block if statement( if-else statement). The general syntax of conditional operator are as:

exp1?exp2:exp3;


Here first of all exp1 will be computed. which is a conditional¬†expression. If exp1 is true then exp2 will be executed. But if exp1 is false then exp3 will be executed. Below is the example on how to use conditional operators instead of using ‘ if ‘ statement.

a=10;
b=5;

if(a>b)
{
	c=a-b;
}
else
{
	c=a+b;
}

The above ‘ if ‘ statement can be used by using the conditional operator in a single c statement as below:

c=(a>b)?a-b:a+b;

The above expression is also called conditional expression.

yashan has written 69 articles

4 thoughts on “Conditional Operators

  1. They used to string em up from lampost cross-bars and then adopt reallty stupid alternative systems of gubbamint when times were really tough.Shape of the future? Oh Dear, How sad for the culprits.I once saw a definition of treason which was all about jeapardising a state's economic or social well-being. Last night I saw Michael Faye pop his head up again.Left me wondering what ever happened to David Ritchwhite.When the shit does eventually hit, I hope they have shoes THIS high, 'cos I wouldn;t want to be in them, and never have

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>