The Conditional Operators in C

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3

What this expression says is: “if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2,otherwise the value returned will be expression 3”. Let us understand this with the help of a few examples:

(a)

int x, y ;

scanf ( “%d”, &x ) ;

y = ( x > 5 ? 3 : 4 ) ;

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.

The equivalent if statement will be,

if ( x > 5 )

y = 3 ;

else

y = 4 ;

 

(b)

char a ;

int y ;

scanf ( “%c”, &a ) ;

y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;

Here 1 would be assigned to y if a >=65 && a <=90 evaluates to true, otherwise 0 would be assigned.

Point to be Noted:

  • It’s not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following examples:

Ex.:

int i ;
scanf ( “%d”, &i ) ;
( i == 1 ? printf ( “Amit” ) : printf ( “All and sundry” ) ) ;

Ex.:

char a = ‘z’ ;
printf ( “%c” , ( a >= ‘a’ ? a : ‘!’ ) ) ;

  • The conditional operators can be nested as shown below.

int big, a, b, c ;

big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;

  • Check out the following conditional expression:

a > b ? g = a : g = b ;

This will give you an error ‘Lvalue Required’. The error can be overcome by enclosing the statement in the : part within a pair of parenthesis. This is shown below:

a > b ? g = a : ( g = b ) ;

In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =. Hence it reports an error.

The limitation of the conditional operators is that after the ? or after the : only one C statement can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional operators aren’t as frequently used as the if-else.

Summary

  • There are three ways for taking decisions in a program. First way is to use the if-else statement, second way is to use the conditional operators and third way is to use the switch
    statement.
  • The default scope of the if statement is only the next statement. So, to execute more than one statement they must be written in a pair of braces.
  • An if block need not always be associated with an else block.However, an else block is always associated with an if statement.
  • If the outcome of an if-else ladder is only one of two answers then the ladder should be replaced either with an else-if clause or by logical operators.
  • && and || are binary operators, whereas, ! is a unary operator.
  • In C every test expression is evaluated in terms of zero and non-zero values. A zero value is considered to be false and a non-zero value is considered to be true.
  • Assignment statements used with conditional operators must be enclosed within a pair of parenthesis.

 

Related Posts

  • 41
    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
    Tags: true, printf, statement, non-zero, operators, control, operator, decision
  • 41
    Decision !!! we all need to alter our actions in the face of changing circumstances. If the weather is fine, then I will go for a stroll. If the highway is busy I would take a diversion. If the pitch takes spin, we would win the match. If she says…
    Tags: statement, decision, control, structure, conditional, if-else, operators, will
  • 36
    We will discuss two operators here that are quite useful in JavaScript: theconditional operator (? :) and the typeof operator. Conditional Operator (? :) The conditional operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result…
    Tags: operator, conditional, operators, true
  • 33
    It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be placed within a pair of braces as illustrated in the below example. Example: The current year…
    Tags: statement, pair, expression, int, control, decision, structure
  • 32
    Operators are the constructs which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Types of Operator Python language supports the following types of operators. Python Arithmetic Operators Assume variable a holds 10…
    Tags: operators, operator

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

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

%d bloggers like this: