Lab # 5: Relational Operators, Logical Operators and Decisions
Lab # 5: Relational Operators, Logical Operators and Decisions
Lab # 5: Relational Operators, Logical Operators and Decisions
Usama Wajhi
Outline
1 Relational Operators
Importance
Definition
Examples
Relational Operators in C++
2 Logical Operators
Importance
Definition
Logical Operators in C++
AND Operator
OR Operator
NOT Operator
Examples
3 Decision Making in C++
Introduction
Decision Making in C++
The if Statement
The if...else Statement
Lab # 5: Relational Operators, Logical Operators and Decisions
Operator Meaning
> Greater than
< Lesser than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Lesser than or equal to
Lab # 5: Relational Operators, Logical Operators and Decisions
Operator Meaning
&& AND
|| OR
! NOT
Lab # 5: Relational Operators, Logical Operators and Decisions
Expression !Expression
false true
true false
Lab # 5: Relational Operators, Logical Operators and Decisions
Decision Making
The if Statement
Flowchart
Algorithm
1 Start
2 Declare variable x
3 Read value of x
4 If x is greater than 100 then display
“The number is greater than 100”
5 Stop
Lab # 5: Relational Operators, Logical Operators and Decisions
The if Statement
Code
1 // this program demonstrates IF statement
2 # include < iostream >
3 using namespace std ;
4 int main ()
5 {
6 int x ;
7
8 cout << " Enter a number : " ;
9 cin >> x ;
10
11 if ( x > 100)
12 {
13 cout << " That number is greater than 100\ n " ;
14 }
15
16 return 0;
17 }
Lab # 5: Relational Operators, Logical Operators and Decisions
Flowchart
Algorithm
1 Start
2 Declare variable x
3 Read value of x
4 If x is greater than 100 then
display “The number is
greater than 100”
5 Else, display “The number is
not greater than 100”
6 Stop
Lab # 5: Relational Operators, Logical Operators and Decisions