Chapter 3 Part - 1
Chapter 3 Part - 1
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
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
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
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.
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:
} Pass
else cout<<"Fail";
else…if Construction
helps user to make decision from among multiple options
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)
// 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:
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
26