Constraints of Switch Statement

Estudies4you
Constraints of Switch Statement
The expression in the switch statement should be reduced to an integer value (ASCII characteristics are integer values).
The case statement should be integer constant (can be ASCII characters).
Conditional Operator Statement
The conditional operator ‘?’ and ‘:’ are sometimes called ternary operators, since they take three arguments.
In fact, they form a kind of shortened, ‘if-then-else’.
The general form is:
                expression1? expression2: expression3
“if expression1 is TRUE (that is, if its value is non-zero), then the value returned will be expression2, otherwise the value returned will be expression3.
It’s not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following example:

Example 1:
int x,y;
scanf(“%d”,&x);
y=(x>5?3:4);

The equivalent If statement will be,
if(x>5)
y=3;
else
y=4;

Example 2:
char a;
int y;
scanf(“%c”,&a);
y=(a>=65 && a<=90?1:0);

Example 3:
if(x>5)
y=3;
else
y=4;
char a=’z’;
printf(“%c”,(a>=’a’?a:’1’));

Example 4: To find the maximum of 3 numbers a,b,c:
int big,a,b,c;
big=(a>b?(a>c?a:c):b>c?b:c));

To Top