You are on page 1of 4

Riphah International University

Faculty of Computing
Programming Fundamentals
Lab 06 Conditional Operator and Switch Statements
Learning Objective
1. Learn basics of C++
2. Learn how to make decisions in C++
3. Learn how to use Switch Statement in C++
4. Learn how to use Conditional Operators in C++

Conditional Operator
Exp1 ? Exp2 : Exp3;

where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of
the colon. The value of a ? expression is determined like this: Exp1 is
evaluated. If it is true, then Exp2 is evaluated and becomes the value of the
entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value
becomes the value of the expression.

The ? is called a ternary operator because it requires three operands and can
be used to replace if-else statements, which have the following form −

if(condition) {
var = X;
} else { var
= Y;
}

For example, consider the following code −

if(y < 10) {


var = 30;
} else { var
= 40;
}

Above code can be rewritten like this −

var = (y < 10) ? 30 : 40;

Here, x is assigned the value of 30 if y is less than 10 and 40 if it is not. You


can the try following example −

#include <iostream>
using namespace std;

int main () {
// Local variable declaration:

Programming Fundamentals Riphah International University


Lab 6: conditional Operators and Switch Statement Faculty of Computing
2

int x, y = 10;

x = (y < 10) ? 30 : 40;


cout << "value of x: " << x << endl;

return 0;
}

When the above code is compiled and executed, it produces the following result

value of x: 40

Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each case.

Syntax
The syntax for a switch statement in C++ is as follows −

switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional

/ you can have any number of case statements.


default : //Optional
statement(s);
}
3

Flow Diagram

Example
#include <iostream>
using namespace std;

int main () {
/ local variable declaration:

char grade = 'D';

switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
4

cout << "Better try again" << endl;


break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;

return 0;
}

This would produce the following result −

You passed
Your grade is D

Lab Tasks
1. Write a program that asks the user to enter two numbers. The program should use the
Conditional operator to determine which number is smaller and which is larger

2. The area of a rectangle is the rectangle`s length times its width. Write a program that asks for
the length and width of two rectangles. The program should tell the users which rectangle has
the greater area. . Solve the task using the conditional Operators.

3. Scientists measure an object mass in kilograms and its weight in newtons. If you know the
amount of mass that an object has, you can calculate its weight in newtons.
Weight = mass * 9.8
write a program that asks the user to enter an object mass and then calculate the weight of an
object. If its weight is more than 1000 newtons then display the message that it’s too heavy.
If its weight is less than 10 newtons then display message that it’s too light. Solve the task
using the conditional Operators.

5. Write a program that asks the user to enter the character from a-z. The program will then
display the user that the character entered is vowel or consonant. Solve the problem using the
switch statements.
4. Write a program that ask the user to enter the number of month. The program will display
the name of the month and the number of days in the month. Solve the problem using the
switch statement.

You might also like