You are on page 1of 26

Loops and Decisions

Chapter (3)

1
Contents
 Relational Operators
 Logical Operators
 Precedence Summary
 Control Statements
 Decision
 Simple if statement

 If…else statement

 Nested if statements

 Else…if construction

 Logical AND(&&) operator


 Logical OR(||) operator
Objectives
 To study about decision statements
 To learn syntax of nested if…else statements and else…if
 To learn the way to handle multiple conditions using nested group of if…else
statements
 To understand the use of logical operators in making decision
Learning Outcomes
 Upon completion of the lecture, the students will have the ability to:
 Use the relational operators to produce the Boolean result by comparing the
values
 Apply the logical operators to determine the logic between variables or values
 Write C++ program that uses selections ( if and if…else statements)
 Write C++ program that uses selections (nested if..else statements)
 Apply else…if construction in rewriting a ladder of nested if…else statements
 Apply the logical operators to replace nested if statements
Relational Operators
 compares two values such as char, int and float or user-defined classes
 result can either be true(1) or false(0).

Table 1: List of C++ Relational Operators


Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
Example of Relational Operators
//compares input number and value “10”
#include <iostream>
using namespace std;
int main()
  { Screen
    int numb; :
Enter a number: 15
    cout << "Enter a number: "; numb<10 is 0

    cin >> numb; numb>10 is 1


numb==10 is 0

    cout << "numb<10 is " << (numb < 10) << endl;
    cout << "numb>10 is " << (numb > 10) << endl;
    cout << "numb==10 is " << (numb == 10) << endl;
    return 0;
  }
Logical Operators
 used to combine two or more relational expressions

Table 2: List of C++ Logical Operators


Operator Name Example
&& AND If one of the expression is false then the result will be false.
|| OR If both conditions are false then the result will be false.
! NOT This operator is used for negation purpose
Precedence Summary
 specifies which kinds of operations will be carried out first
 operators on the same row have equal precedence
Table 3: Precedence of Operators
Operator Type Operators Precedence
Grouping Operator () Highest
Unary !, ++, --, +, -
*, /, %
Arithmetic
+, -
<, >, <=, >=
Relational
==, !=
&&
Logical
||
Conditional ?:
Assignment =, +=, -=, *=, /=, %= Lowest
Precedence Summary (Example)

5 1 2 4 3
int n = 6;
bool x = n / 2 < 5 && n == 3;
x = n / 2 < 5 && n == 3

x = 6 / 2 < 5 && 6 == 3
1 3

3 0
2

1
x
4
0 5
0
Control Statements
 program statements that cause the flow of control jumps from one part
of the program to another
 depending on calculations performed in the program

 Two major categories:


 Loops
 Decisions
 repeats actions while a condition remains true in loop structure
 takes one of the courses of action in decision structure
Decisions
 causes a one-time jump to a different part of the program, depending on
the value of an expression

 Several ways of decision


 Simple if statement
 if…else statement, which chooses between two alternatives
 if…else if ladder, which chooses from among multiple options
 switch statement, which creates branches for multiple alternative
sections of code
 conditional operator, which is used in specialized situations
Simple if Statement
Syntax:
Test expression
if (condition)
statement; //single-statement
Test
False True
condition
 
(or)
Body of if

if (condition)
{
statement 1; Multiple-statement if
statement 2; body Exit
…………..
statement n;
Note: no semicolon Figure 1: Operation of the if statement
}
here
Simple if Statement (Example)
//demonstrates if statement
Memory:

#include<iostream> x
using namespace std; 200
int main()
{
Screen:
int x;
cout<<"Enter a number: "; Enter a number: 200
cin>>x;
if(x>100) That number is greater than 100
cout<<"That number is greater than 100\n";
return 0;
}
Multiple Statements in the if Body
//demonstrates if with multiline body

#include<iostream> Memory:
using namespace std; x
int main() 101
{
int x;
Screen:
cout<<"Enter a number: ";
cin>>x; Enter a number: 101
if(x>100) The number 101 is greater than 100
{
cout<<"The number "<<x;
cout<<" is greater than 100\n";
}
return 0;
}
The if…else Statement
Syntax:
if (condition)
statement; //single-statement if body
else
statement; //single-statement else body
Test
False True
(or) condition
 
if (condition)
{ Body of else Body of if

statement; Multiple-statement if
statement; body
}
else Exit
{ Multiple-statement else
statement; body
Figure 2: Operation of the if…else statement
statement;
}
The if…else Statement (Example-1)
//demonstrates if…else statement
#include<iostream> Memory:
using namespace std; x
int main() 105
{
int x;
cout<<"Enter a number: "; Screen:
cin>>x;
if(x > 100) Enter a number: 105
That number is not greater than 100
cout<<"That number is greater than 100\n";
else
cout<<"That number is not greater than 100\n";

return 0;
}
The if…else Statement (Example-2)
//demonstrates if…else statement
#include<iostream>
using namespace std;
int main()
{ Memory:
int num;
num
cout<<"Enter a number: ";
cin>>num; 10

if(num>=0) Screen:
cout<<"It is positive.\n";
Enter a number: 10
else
It is positive.
cout<<"It is negative.\n";

return 0;
}
Nested if Statements
 An if…else statement can be nested inside an if…else statement, which
is nested inside another if…else statement.

False Test True


Syntax: condition1
 
if (condition1) Body of else1
False Test True
{ condition2
 
if(condition2) Body of else2 Body of if 2
statement1;
else
statement2;
} Exit

else
statement3; Figure 3: Operation of the nested if statement
Nested if Statement (Example)
//checks the marks to determine whether the student passes or fails the exam
int mark1,mark2,mark3;
cout<<"Enter three marks: ";
cin>>mark1>>mark2>>mark3;
Memory:
if (mark1 >= 50) {
mark1 mark2 mark3
if (mark2 >= 50) {
63/40 67 54
if (mark3 >= 50) cout<<"Pass";
else cout<<"Fail";
} Screen:

else cout<<"Fail"; Enter three marks: 63 67 54

} Pass

else cout<<"Fail";
else…if Construction
 helps user to make decision from among multiple options

Syntax: False Test


condition1
True

if (condition1)  
Statement1
statement1; False Test
condition2
True

else if (condition2)  
Statement2
statement2; False Test True
condition3
else if(condition3) Statement
 
Statement3
statement3;
:
:
else Exit

statement;
Figure 4: Operation of the if…else statement
else…if Construction (Example)

// tests a number whether it is positive or negative or zero


Memory:
int num; num
cout<<"Enter a number : "; 0
cin>>num;
0<
> 0 (false)
if(num>0)
cout<<"It is positive number.";
Screen:
else if(num<0)
cout<<"It is negative number."; Enter a number: 0
else It is zero.
cout<<"\nIt is zero.";
Logical AND (&&) Operator (Example)

// checks the marks to determine whether the student passes or fails the exam
mark1 mark2 mark3
int mark1,mark2,mark3; 70 80 46
cout<<"Enter three marks: ";
cin>>mark1>>mark2>>mark3; mark1 >= 50 && mark2 >=50 && mark3 >=
50
true true false
if (mark1 >= 50 && mark2 >=50 && mark3 >= 50)
cout<<"Pass"; true
else false
cout<<"Fail";
Screen:

Enter three marks: 70 80 46


Fail
Logical AND (&&) Operator (Cont’d)

False mark1 >= 50 True


?

output “Fail” False mark2 >= 50 True


?

output “Fail” False mark3 >= 50 True


?

output “Fail” output “Pass”

Figure 5: Equivalent nested if statements


Logical OR (||) Operator (Example)

ch
//checks whether the input character is vowel or not
o
char ch;
ch=='a' || ch=='e' || ch=='i' || ch=='o'
cout<<"Enter a character: "; false false false true
cin>>ch; …….
false
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
cout<<"vowel"; false
else
cout<<"not vowel"; true

Screen:
Enter a character: o
vowel
Reading Assignments
1. Chapter(3) - Precedence: Arithmetic and Relational Operators (page.89-90)
Reference book : “Object-Oriented Programming in C++” by Robert Lafore, 4th Edition
Download link :
https://docs.google.com/file/d/0B21HoBq6u9TsUHhqS3JIUmFuamc/view

2. Chapter(3) - Matching the else (page.104-105)


Reference book : “Object-Oriented Programming in C++” by Robert Lafore, 4th Edition
Download link : https://docs.google.com/file/d/0B21HoBq6u9TsUHhqS3JIUmFuamc/view
Thank You!

26

You might also like