Skip to content

Conditional Operators

by yashan on August 10th, 2011

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.

From → C Tutorials

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS