The Conditional Operator: ?

I l @ ve RuBoard

The Conditional Operator: ?

C offers a shorthand way to express one form of the if else statement. It is called a conditional expression and uses the ?: conditional operator. This is a two-part operator that has three operands. Here is an example that yields the absolute value of a number:

 x = (y < 0) ? -y : y; 

Everything between the = and the semicolon is the conditional expression. The meaning of the statement is this: If y is less than zero, then x = -y ; otherwise , x = y . In if else terms, the meaning can be expressed as follows :

 if (y < 0)     x = -y; else     x = y; 

This is the general form of the conditional expression:

  expression1 ? expression2 : expression3  

If expression1 is true (nonzero), then the whole conditional expression has the same value as expression2 . If expression1 is false (zero), the whole conditional expression has the same value as expression3 .

You can use the conditional expression when you have a variable to which you want to assign one of two possible values. A typical example is setting a variable equal to the maximum of two values:

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

This sets max to a if it is greater than b , and to b otherwise.

Usually, an if else statement can accomplish the same end as the conditional operator. The conditional operator version, however, is more compact and, depending on the compiler, may result in more compact program code.

Let's look at a paint program example, shown in Listing 7.8. The program calculates how many cans of paint are needed to paint a given number of square feet. The basic algorithm is simple: Divide the square footage by the number of square feet covered per can. Suppose the answer is 1.7 cans, however. Stores sell whole cans, not fractional cans, so you would have to buy two cans. Therefore, the program should round up to the next integer when a fractional paint can is involved. The conditional operator is used to handle that situation, and is also used to print cans or can , as appropriate.

Listing 7.8 The paint.c program.
 /* paint.c -- uses conditional operator */ #include <stdio.h> #define COVERAGE 200       /* square feet per paint can */ int main(void) {   int sq_feet;   int cans;   printf("Enter number of square feet to be painted:\n");   while (scanf("%d", &sq_feet) == 1)   {        cans = sq_feet / COVERAGE;        cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1;        printf("You need %d %s of paint.\n", cans,             cans == 1 ? "can" : "cans");        printf("Enter next value (q to quit):\n");   }   return 0; } 

Here's a sample run:

 Enter number of square feet to be painted:  200  You need 1 can of paint. Enter next value (q to quit):  215  You need 2 cans of paint. Enter next value (q to quit):  q  

Because the program is using type int , the division is truncated; that is, 215/200 becomes 1 . Therefore, cans is rounded down to the integer part. If sq_feet % COVERAGE is , then COVERAGE divides evenly into sq_feet and cans is left unchanged. Otherwise, there is a remainder, so 1 is added. This is accomplished with the following statement:

 cans += ((sq_feet %prcnt; COVERAGE == 0)) ? 0 : 1; 

It adds the value of the expression to the right of += to cans . The expression to the right is a conditional expression having the value or 1 , depending on whether COVERAGE divides evenly into sq_feet .

The final argument to the printf() function is also a conditional expression:

 cans == 1 ? "can" : "cans"); 

If the value of cans is 1 , then the string " can " is used. Otherwise, " cans " is used. This demonstrates that the conditional operator can use strings for its second and third operands.

Summary: The Conditional Operator

The Conditional Operator:

 ?: 

General Comments:

This operator takes three operands, each of which is an expression. They are arranged as follows:

  expression1 ? expression2 : expression3  

The value of the whole expression equals the value of expression2 if expression1 is true. Otherwise, it equals the value of expression3 .

Examples:

 (5 > 3) ? 1 : 2 has the value 1. (3 > 5) ? 1 : 2 has the value 2. (a > b) ? a : b has the value of the larger of a or b. 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net