Conditional Statements in with C++
08/10/2024 1
Goals
Nested if…else Statements
Logical Operators
Compound Statements
Switch Statement
Nested Switch Statement
Ternary/Conditional Operator
08/10/2024 2
Decision Making
08/10/2024 3
Nested “if...else” Statements
• Purpose: To test more than
one factor before we write our
executable code
• By nesting if structures, we
write ONE COMPLETE if
structure inside a SINGLE
BRANCH of a parent if structure
08/10/2024 4
Flow chart of Nested “if...else”
Nested “if...else” Statements
if (marks>90){
cout<<“You got A grade”;
if(Available_scholorships>0)
{
cout<<“You got scholarship too”;
tution_fee_due = 0;
}
}
else {
if (marks>=50)
cout<<“You passed the course”;
else
cout<<“You failed the course”; }
08/10/2024 6
Nested “if...else” Statements
int i = 10;
if (i == 10){
// First if statement
if (i < 15)
OUTPUT
cout<<"i is smaller than 15\n";
// Nested - if statement
i is smaller than 15
// Will only be executed if the above statement is true
if (i < 12)
i
cout<<"i
else
is smaller
is smaller than
than 12 12 too
too\n";
cout<<"i is greater than 15"; }
}
08/10/2024 7
Nested “if...else” Statements
int main()
{
int a, b, c;
cout<<“Enter three numbers, a, b, and c:\n”;
cin >> a >> b >> c;
if( a==b )
if( b==c )
cout << “a, b, and c are the same\n”;
else
cout << “a and b are different\n”;
return 0;}
08/10/2024 8
Take 3 values and print them
in ascending order by comparing values
Using Nested if-else.
Logical Operators
Used to create relational expressions from other relational expressions
New relational expression is true if both
expressions are true
New relational expression is true if either
expression is true
Reverses the value of an expression; true
expression becomes false, false expression
becomes true
08/10/2024 11
Logical Operator Rules
Operand(s) must be bool(true/false)
true && true
true || true
true || false true
false || true
! false
true && false
false && true false
false || false
! true
08/10/2024 12
Logical Operator Examples
int x = 12, y = 5, z = -4;
•
(x > y) && (y > z) true
(x > y) && (z > y) false
(x <= z) || (y == z) false
(x <= z) || (y != z) true
!(x >= z) false
08/10/2024 13
Compound Conditions
COMPOUND: Multiple conditions
if ( (Age < 0) || (Age > 120) )
◦ At least one condition must be true
if ( (Age >=1) && (Age <= 120) )
◦ BOTH conditions must be true
INVALID:
if ( Age >=1 && <= 120 ) //Need 2 relational expressions
if ( 1 <= Age <= 120 ) //Although okay in math!
08/10/2024 14
Compound Conditions(Example)
Write a program that inputs three numbers and prints the maximum numbers
if(( a > b) && ( a > c))
cout<<a<<" is the maximum number\n";
else if (( b > a ) && (b > c))
cout<<b<<" is the maximum number\n";
else
cout<<c<<" is the maximum number \n";
08/10/2024 15
Class Task
Write a C++ program that accepts the application base on the following two conditions.
•4 or more years of college, AND
•2 years experience programming in C++ OR a grade point average greater than 3.5.
if ( college >= 4 && ( experience >= 2 || gpa > 3.5 ) )
cout<<"Interview applicant";
else
cout<<“ CV Not shortlisted ”;
08/10/2024 16
Precedence of logical operators
08/10/2024 18
Operator Precedence
Solve the expression
a > b && 45 <= sum || sum < a + b && d > 90
Sol: Assume a = 4 , b = 5, d = 6 , sum = 14
4 >5 && 45<=14 | | 14 < 4+5 && 6>90
4 >5 && 45<=14 | | 14 < 9 && 6>90
0 && 0 || 0 && 0
0 || 0
0 False Condition
08/10/2024 19
Class Task
Write the answer to the following expression manually to show
how operator precedence groups operands:
a > b && 45 <= sum || sum < a + b && d > 90
Note: Don't change the meaning of the expression; use
parentheses to clarify the order of evaluation.
08/10/2024 20
switch statement
Provides a series of alternatives (selections) based on the value of a SINGLE
variable
Replaces a series of chained if-else statements
Makes code easier to read
08/10/2024 21
Switch - Syntax
switch (Variable)
{
case <value_1> :
statement1;
statement2;
break;
case <value_2> :
statement1;
statement2;
break;
case <value_3> :
statement1;
break;
default:
statement1; Optional
statement2;}
08/10/2024 22
switch statement (without break)
Suppose ch is 'a’
switch (ch) {
case 'a': cout <<“ ch contains a”;
case 'b': cout <<“ ch contains b”;
case 'c': cout <<“ ch contains c”;
}
08/10/2024 23
Switch statement (with break)
Suppose ch is ‘b’
switch (ch) {
case 'a': cout <<“ ch contains a”; break;
case 'b': cout <<“ ch contains b”; break;
case 'c': cout <<“ ch contains c”; break;
}
cout<<“\n End of program…”;
08/10/2024 24
Switch – Example-1
char grade;
cin>>grade;
switch (grade) {
case ‘A’:
tution_fees *= 0.20;
break;
case ‘B’:
tution_fees *= 0.40;
break;
case ‘C’:
tution_fees *= 0.60;
break;
default:
tution_fees *= 1;}
08/10/2024 25
Switch – Example-2
int day;
cout<<“Enter day number“;
cin>>day;
switch (day)
{
case 1 :
cout << "This is a weekend day";
break;
case 7 :
case 3 :
case 6 : cout << "This is a weekday"; break;
default : cout << "Not a legal day"; }
08/10/2024 26
Switch – Example-3
int n1, n2;
char op;
cout<<“Enter numbers: “;
cin>>n1>>n2;
cout<<“Enter an arithmetic operator”;
cin>>op;
switch (op) {
case ‘+’ :
cout<<(a+b); break;
case ‘-’ :
cout<<(a-b); break;
case ‘*’ :
cout<<(a*b); break;
case ‘/’ :
cout<<(a/b); break;
default:
cout<<“Error: Invalid key…"; }
08/10/2024 28
Nested Switch
• switch can also be nested…
• Another switch part of the case component
• Example…
08/10/2024 30
Example: Nested switch
Ternary/Conditional Operator
The conditional operator ( ? : ) is a ternary operator
(three operands), is used to simplify an if/else
statement.
condition ? expression2 : expression3;
If expression1 is true, the result of the conditional expression is expression2.
Otherwise, the result is expression3
08/10/2024 32
Conditional Operator (Ternary operator)
if (x > 0)
y = 1;
else
y = -1;
is equivalent to
Y=(x > 0) ? 1 : -1;
Syntax:
result = (condition) ? Expression1 : Expression2;
08/10/2024 33
Conditional Operator, examples
cout << ((num % 2 == 0) ? "num is even" : "num is
odd");
int min_value = (num1<num2) ? num1 : num2;
08/10/2024 34
Nested Ternary Operator
Just like if…, ternary operator can be nested too
Syntax:
result = (condition) ? Expression1 : Expression2;
This can be another
test condition …
08/10/2024 35
Examples…
a ? b : c;
if ( a )
b;
else
c;
08/10/2024 36
Examples…
//conditions are in red
a ? b : c ? d : e ? f : g ? h : i;
if (a)
b;
else if (c)
d;
else if (e)
f;
else if (g)
h;
else
i;
08/10/2024 37
Exercise-1
What will be the output of the following code?
int marks= 2;
switch(marks)
{
case 1: cout<<”You are in year-1”;
case 2: cout<<”You are in year-1”;
case 3: cout<<”You are in year-2”;
case 4: cout<<”You are in year-2”;
}
08/10/2024 38
Exercise-2
What will be the output of the following code?
int n = -29;
int temp = (n<0)?-n:n;
cout<<temp;
08/10/2024 39
Exercise-3
What will be the output of the following code?
int x=21, y=31, z=44;
if(x>16 && y>x || z%2==0 )
cout<<“Hello“;
else
cout<<“World!“;
08/10/2024 40
Exercise-4
What will be the output of the following code?
int a = 5, b = 30;
if(a > b)
if(b > 0)
a = a + 5;
else
if(b >= 30)
b = b * 10;
cout<<”a=”<<a<<” ”<<”, b=”<<b;
08/10/2024 41
Exercise-5
What will be the output of the following code?
int x = 5, y = 30;
if(y/x > 2)
if(y % x !=2)
x = x + 2;
cout<<x<<”\n“<<y;
08/10/2024 42
Exercise-6
Write a program that ask the user to enter a number in the
range of 1—10.
Use a switch statement and display corresponding Roman
Number against each entered decimal value. E.g.,
1I
2 II
3 III
4 IV
08/10/2024 43
Exercise-8
Write a program that calculates the person’s Body Mass Index (BMI). The BMI is
often used to determine if a person is overweight, or underweight for his/her
height.
BMI is calculated as:
BMI = (weight * 703) / (height*height)
weight is in pounds and height is in inches.
The program should display the message:
- “Optimal Weight” if BMI is 18.5—25
- “Under Weight” if BMI is < 18.5
- “Over Weight” if BMI is > 25
08/10/2024 44
References
Text book: Starting out with c++
08/10/2024 45