Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Birla Institute of Technology & Science Pilani, Hyderabad

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.

The switch statement has the following form:

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.

Ex-1 Consider the program (calc.c) using switch-case conditional construct.

#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;
}

a) Replace the statement “res = (float)num1/(float)num2” of the above program


by “res = (float)num1/num2” and observe what happens. Does the output change? Why?
b) Remove all the break statements from the program and try to execute the program with
few inputs. Observe the difference.

1. Nested if

if (Condition-1)

{ /* if condition-1 is true this block of statements get executed */


if (Condition-2)

block of statements executed when “if” condition-2 is true;

else

block of statements executed when “if” condition-2 is false;

else

/* if condition-1 is false this block of statements get executed */

if (Condition-3)

block of statements executed when “if” condition-3 is true;

else

block of statements executed when “if” condition-3 is false;

Example 2:
/* C program to illustrate If statement */

/* C program to illustrate Nested If statement */

#include <stdio.h>
int main()
{
int a =10;

if(a >= 10)


{
if(a > 10)
{
printf(“a is greater than 10”);
}
else
{
printf(“a is equal to 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.

Syntax: expression1 ? 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”.

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.

The equivalent if statement will be,

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" ) ) ;

Ex.: char a='z';

printf ( "%c" , ( a >= 'a' ? a : '!' ) ) ;

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

0.0–0.99 Failed semester—registration suspended

1.0–1.99 On probation for next semester

2.0–2.99 (no message)

3.0–3.49 Dean’s list for semester

3.5–4.00 Highest honors for semester

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?

You might also like