1
Programming with C++
Lecture 4: Decision making statements
Sikander Naseem
2
Decision making statements
Sequential Programming
Using decision making statements we can have
flexible programming.
Condition base execution
• The if statement
• The if ---- else statement
3
The if Statements
Syntax
If (condition) statement;
Condition is any integral expression
Statement is any executable statement.
The statement will be executed if the integral
expression is having any non zero value (true)
If the value from the integral expression is zero
(false) the statement will not be executed
4
The if Statements
int main()
{int m, n;
cout << "Enter integer m = ";
cin>>m;
cout << "Enter integer n = ";
cin>>n;
if (m%n) cout << "The remainder is not
zero"<<endl;
}
Since the execution ends without anything on false
condition so it incomplete
So…
5
The if ---- else Statements
The if--- else statement gives choice between two
statements depending on the condition
Syntax
if (condition) statement 1;
else statement 2;
IF the value of condition is non zero statement 1
will be else (zero) than statement 2 will be excuted
6
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter integer m = ";
cin>>m;
cout << "Enter integer n = ";
cin>>n;
if (m%n) cout << "The remainder is not zero"<<endl;
else cout << "The remainder is zero"<<endl;
}
7
Comparison Operators
Six comparison operators
X < y //x is less than y
X > y //x is greater than y
X <= y //x is less than or equal to y
X >= y //x is greater than or equal to y
X == y //x is equal to y
X != y //x is not equal to y
These comparison operators can be used as condition
in the decision making statements.
For instance an integral statement with comparison of
two values.
Assignment and equal to operator
8
Comparison Operators
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m<n) cout << "m is the smaller integer"<<endl;
else cout << "n is the smaller integer"<<endl;
}
What if we put two equal nr.s
9
Comparison Operators
Three integers
int main()
{int l, m, n;
cout << "Enter three positive integer = ";
cin>>l>>m>>n;
int min=l;
if (m<min) min=m;
if (n<min) min=n;
cout << min<< "is the smallest integer"<<endl;
}
10
Statement block
Remember internal blocks and scope
Statement block are used to execute several
statements with one decision.
if (condition){ statement 1; statement 2; statement 3;}
11
Statement block
int main()
{// this program arranges two integer in ascending order
int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m>n) {int arrange=m; m=n; n=arrange;} //Swap
cout <<m<<" and "<< n<<endl;
}
12
Compound conditions
With compound conditions we can combine two
condition together for decisions making,
The OR (||), AND (&&) and NOT (!) operators are
used for this purpose.
Truth Table
For example
x<y && x!=y
x<y || a==b
Short circuiting
13
Compound conditions
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m<n && m!=n) cout << "m is the smaller integer"<<endl;
if (n<m && m!=n) cout << "n is the smaller integer"<<endl;
}
14
Compound conditions
int main()
{int l, m, n;
cout << "Enter three positive integer = ";
cin>>l>>m>>n;
if (l<=m && l<=n) cout<<l<<"is the smallest
integer";
if (m<=l && m<=n) cout<<m<<"is the smallest
integer";
if (n<=l && n<=m) cout<<n<<"is the smallest
integer";
}
15
Nested decision making statements
Just like compound statement we can combine two
conditions together by nesting the decision making
statement.
Selection appropriate comparison condition is tricky as
in nesting the compounding is always AND operator.
if (condition1)
if (condition2) statement 1
if (condition1)
if (condition2) statement 1;
else statement 2;
16
Nested decision making statements
int main()
{int l, m, n;
cout << "Enter three positive integer = ";
cin>>l>>m>>n;
if (l<=m)
if (l<=n) cout<<l<<"is the smallest integer";
if (m<=l)
if (m<=n) cout<<m<<"is the smallest integer";
if (n<=l)
if (n<=m) cout<<n<<"is the smallest integer";
}
All equal integers?
17
The else … If construct
Test for parallel alternatives with nesting
The else --- if construct can be used.
if (condition1) statement 1;
else
if (condition2) statement 2;
if (condition1) statement 1;
else if (condition2) statement 2;
else if (condition3) statement 3;
18
The else … If construct
int main()
{int marks;
cout << "Please enter your score out of 100 = ";
cin>> marks;
if (marks>100) cout <<"out of range value";
else if (marks>=90) cout <<" your grad is A";
else if (marks>=80) cout <<" your grad is B";
else if (marks>=70) cout <<" your grad is C";
else if (marks>=60) cout <<" your grad is D";
else if (marks>=0) cout << " your grad is F";
else cout << "out of range value";
} switch statement
The
19
Practice
Create a program to ask shape of room from round and
rectangular/square. Than ask the user for dimensions. Calculate
the areas and calculate the carpet cost for 100 Rs/ft2. Output total
cost.
Convert the grading software in above example to be flexible.
Such that acquired marks and max marks are asked for input and
giving grades based on score in 100%
Create a simple calculator. Enter two integers first. Use else---if
construct to select the athematic operator (+, -, *, /). Output the
answer after the athematic operations is performed.
20
The End
Thanks for coming
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi