Professional Documents
Culture Documents
Switch and If-Else Statements
Switch and If-Else Statements
Campus
Computer Programming (CS F111) 2021-22 Second
semester
Lab-4
1. The ‘switch’ Statement
The switch statement is provided by C to select one of several alternatives. The switch
statement is especially useful when the selection is based on the value of a single variable
or of a simple expression (also called the controlling expression). The value of this
expression may be char or int but not double.
switch(expression){
case expression 1:
statements to be executed if expression 1 matches
expression
break; /* DO NOT FORGET BREAK. ELSE CODE WILL SIMPLY
FALL THROUGH THE REST OF THE CASES */
case expression 2:
statements to be executed if expression 2 matches
expression
break;
………
case expression n:
statements to be executed if expression n matches
expression
break;
default:
statements to be executed if no case expression matches
expression
}
Here expression results in an integer value and can be of type char also. Each of
expression-1, expression-2, …, expression-n represents constant integer-valued
expressions. break is not mandatory here.
#include <stdio.h>
#include <math.h>
int main()
{
int num1, num2;
float res;
char op;
printf ("Enter the first number: ");
scanf("%d",&num1);
printf ("Enter the second number: ");
scanf("%d",&num2);
printf ("Enter the operator (+ , / , ^): ");
scanf (" %c",&op);
switch (op){
case '+':
res = num1 + num2;
break;
case '/':
res = (float)num1/(float)num2;
break;
case '^':
res = pow(num1, num2);
break;
default:
printf("Invalid Operator\n");
return 0;
}
printf("num1 %c num2 = %f\n",op,res);
return 0;
}
1. Nested if
if (Condition-1)
else
else
if (Condition-3)
else
Example 2:
/* C program to illustrate If statement */
#include <stdio.h>
int main()
{
int a =10;
}
else
{
printf(“if a is less than 10”);
}
return 0;
}
Conditional operator:
Sometime instead of if else, the conditional operator ? : could be used to perform one of the two
actions. “? :” is called a ternary operator in C.
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”.
Example 3:
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.
if ( x > 5 )
y=3;
else
y=4;
Example 4:
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" ) ) ;
Exercise Questions:
Q:1 Write an if statement that might be used to compute and display the average
of the five numbers, whose sum is stored in variable total. This average should be found only if all
the given numbers are greater than 0 and they are even; otherwise, an error message should be
displayed.
Q:2 Write an interactive program using switch case to compute the area of a square ( area=
side*side ) or a circle ( area= 3.14*radius*radius ) or an rectangle (area= length*width ), after
prompting the user to type the first character of the figure name (S or C or R).
Q:3 Implement the following decision table using a nested if statement. Assume that the grade
point average is within the range 0.0 through 4.0.
Grade Point Average Transcript Message
Q:4 Write a program to take three numbers and sort them in ascending order.
Sorting a sequence of numbers is arranging the numbers in ascending or descending order.
Q:5 Write a program to find the largest among the two numbers using ternary operators and using
If statements?