0% found this document useful (0 votes)
24 views5 pages

Lab 6 Decision Making Ii: 6.1 Objectives

The document discusses logical operators, nested if-else statements, and switch-case statements. It provides examples of using these statements to evaluate conditions and execute code based on different cases. It also includes an activity for students to write a program that checks if a number is even or odd using a switch-case statement.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Lab 6 Decision Making Ii: 6.1 Objectives

The document discusses logical operators, nested if-else statements, and switch-case statements. It provides examples of using these statements to evaluate conditions and execute code based on different cases. It also includes an activity for students to write a program that checks if a number is even or odd using a switch-case statement.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB 6

DECISION MAKING II
6.1 OBJECTIVES

- To get familiar with the use of logical operators


- To be able to use nested branches of if-else statement
- To learn and practice switch-case statement

6.2 PRE-LAB READING

6.2.1 LOGICAL OPERATORS

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.

Table 1 Logical Operators

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 if(marks>=60 && marks<80)


{
cout<<"You have got B grade"<<endl;
}

else if(marks>=40 && marks<60)


{
cout<<"You have got C grade"<<endl;
}

else if(marks>=0 && marks<40)


{
cout<<"You are Fail"<<endl;
}

else
{
cout<<"Invalid Input"<<endl;
}

return 0;
}

6.2.2 NESTED if-else BRANCHES

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”;

if ( number <50) //inner if-block


{
cout<< “ and number is also less than 50”;
}
else
{
cout<< “ but the number is not less than 50”;
}
}
else
{
cout<< “No the number is not less than 100”;
}
return 0 ;
}

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.

Figure 1 Flow chart of switch-case

Let’s make it clear by checking the following example:

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.

Every branch of the switch must be


terminated by a break instruction. If the
break is missing, execution keeps going to
the next branch, and so on.

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.

You might also like