Lab 6 Decision Making Ii: 6.1 Objectives
Lab 6 Decision Making Ii: 6.1 Objectives
DECISION MAKING II
6.1 OBJECTIVES
Sometimes decision making depends on more than one Boolean expression/statement at a time. In
such cases logical operators are used to combine two or more relational expressions.
For example consider a program which asks the user to enter his marks and assigns the grade according
to the following criterion.
Grade A: 80-100
Grade B: 60-79
Grade C: 40-59
Fail: below 40
#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter your marks(out of 100)"<<endl;
cin>>marks;
if(marks>=80 && marks<=100)
{
cout<<"You have got A grade"<<endl;
}
else
{
cout<<"Invalid Input"<<endl;
}
return 0;
}
You can write any if-else block inside another if-else. This is called nesting of if-else branches. For
example, to enter in a locked room, you need to open the lock first then obviously the door. Note that, it
is not possible to open the door before unlocking it. While doing nesting we handle such situations.
Consider a program which takes a number from the keyboard and checks whether that number is less
than 100 or not. If it is less than 100 than again checks is it less than 50 or not. To do so, the code will be
using nested if-else as:
#include <iostream>
using namespace std;
int main()
{
int number ;
cout << “Enter an integer\n”;
cin >>number;
if ( number <100 ) //outer if-block
{
cout<< “Yes the number is less than 100”;
You have learnt so far that how to use if-else for single and multiple selection statements. There is
another way to make selection from multiple statements which is known as switch statement.
Sometimes, the switch statement reduces the complexity to some extent. It keeps a check on value
returned by a single expression and then executes respective code block.
6.2.3 switch STATEMENT
Switch statement compares the value of an expression against a list of integers or character constants.
The list of constants are listed using case statement along with a break statement to end the execution.
If no condition matches then the code under default statement is executed.
switch(expression)
{
case constant1:
Statements;
break;
case constant2:
Statements;
break;
.
.
default:
Statements;
}
The break; statement causes the program to proceed to the first statement after the switch
structure.
int main()
{
char grade = 'B';
switch (grade)
{ // switch block starts here
case 'A' :
cout << "Outstanding" <<endl;
break;
case 'B' :
cout << "Excellent" <<endl;
break;
case 'C' :
cout << "Very Good" <<endl;
break;
case 'D' :
cout << "Average" <<endl;
break;
case 'E' :
cout << "Below Average" <<endl;
break;
case 'F' :
cout << "Fail" <<endl;
break;
default :
cout << "Invalid Grade" <<endl;
}
//switch ends
return 0;
} //main ends
After seeing break keyword the control goes immediate after the switch block i.e at return 0; statement
in this example. For instance if the value assigned doesn’t match with any of the case labels (A-F) then
the default clause is considered.
Activity:
Can you write a program which asks the user to enter a number and then checks whether it is even or
odd? You are required to use only switch-case statement.
Ensure that the program terminates displaying an error message when a wrong input is given by
the user.