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

3 thoughts on “Conditional Operators

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>