Switch: Decision and Control Statements: Special Math Functions
Switch: Decision and Control Statements: Special Math Functions
1
switch statement
The switch statement is a multiway conditional
statement generalizing the if-else statement.
• It is a far neater way of writing multiple if
statements.
The switch statement evaluates the value of an
expression and branches to one of the case
labels.
• Duplicate labels are not allowed, so only one case
will be selected.
• The expression must evaluate an integer,
character or enumeration.
2
switch statement
Switch Structure
( Multiple Selection )
T
Break
F
T
Break
T
Break
3
switch statement
switch (expression)
{
case constant1: statement1;
case constant2: statement2;
...
case constantn: statementn;
default: default_statement;
}
4
switch statement
The case labels can be in any order and
must be constants.
5
The effect of a switch
1. Evaluate a switch expression.
2. Go to the case label having a constant value
that matches the value of the expression
found in step 1. If a match is not found, go to
the default label; if there is no default label,
terminate the switch.
3. Terminate the switch when a break is
encountered, or by “falling off the end”.
6
default statement
The default label can be put anywhere in the
switch.
• When C sees a switch statement, it evaluates the
expression and then looks for a matching case
label.
• If none is found, the default label is used.
• There may be at most one default label in a switch.
• Typically, it occurs last although it can occur
anywhere.
7
An Example
switch (color)
{
case ‘R’: printf(“red”);
case ‘G’: printf(“green”);
case ‘B’:printf(“blue”);
default: printf(“Other Color”);
}
8
break statement
Typically, the last statement before the next
case or default label is a break statement.
A break statement inside a switch means
that execution will continue after the switch
statement.
• If a break statement is not there, execution
“falls through” to the next statement.
The break statement interrupts the normal
flow of control.
9
break statement
switch (expression)
{
case constant1: statement1;
break;
case constant2: statement2;
break;
.......
case constant n: statementn;
break;
default: default_statement;
}
10
break statement
switch (no)
{
case 1: printf(“Try a larger No”);
break;
case 2: pintf(“You are the Winner”);
break; // exit switch statement
...
case 3: printf(“Try a Smaller No”);
break;
default: printf(“Invalid No”);
}
// break jumps to here
11
Converting if to switch
Consider a series of nested if statements:
char operator;
double a, b, result;
....
if (operator == ‘+’) result = a + b;
else if (operator == ‘-’) result = a – b;
else if (operator == ‘*’) result = a * b;
else if (operator == ‘/’)
{
if (b == 0) printf(“Error: Divide by zero\n”);
else result = a / b;
}
else printf(“Unknown operator %c\n”, operator);
12
Converting if to switch
char operator;
double a, b, result;
switch (operator)
{
case ‘+’: result = a + b;
break;
case ‘-’: result = a - b;
break;
case ‘*’: result = a * b;
break;
case ‘/’: if (b == 0)
printf(“Error: Divide by zero”);
else result = a / b;
break;
default : printf(“Unknown operator %c”, operator);
}
13
Specialized Math Functions
Apart from the standard functions found
in the C math library (math.h), there are
also a number of more specialized
functions.
14
ceil
The function ceil(x) returns the smallest
integer greater than or equal ot x:
x = 5.7
z = ceil(x);
would cause the variable z to be assigned
a value which is the ceiling of x,
i.e. z = 6.0
15
floor
The function floor(x) returns the largest
integer smaller than or equal to x:
x = 5.7
z = floor(x);
would cause the variable z to be
assigned a value which is the floor of x,
i.e. z = 5.0
16
modf
The function modf(x,&i) breaks x into integer
and fractional parts:
x = 5.7
z = modf(x,&i);
would cause the variable z to be assigned the
value f and indirectly the value i, so that:
x=i+f
hence, z = 5.0, and i =0.7
17
Leaving the Program
There are a number of ways to leave the
program, all found in the standard library
stdlib.h.
18
abort and exit
The function abort() causes abnormal program
termination.
abort();
The function exit(status) causes normal
program termination.
exit(status);
If the value of status is zero, the host
environment assumes that the program executed
successfully, for all other values it assumes the
program did not execute successfully.
19
The End
20