Lecture 5, 6
Lecture 5, 6
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";
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
08/10/2024 11
Logical Operator Rules
Operand(s) must be bool(true/false)
08/10/2024 12
Logical Operator Examples
int x = 12, y = 5, z = -4;
•
(x <= z) || (y == z) false
(x <= z) || (y != z) true
08/10/2024 13
Compound Conditions
COMPOUND: Multiple conditions
if ( (Age < 0) || (Age > 120) )
◦ At least one condition 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
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.
cout<<"Interview applicant";
else
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
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:
08/10/2024 20
switch statement
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
• Example…
08/10/2024 30
Example: Nested switch
Ternary/Conditional Operator
08/10/2024 32
Conditional Operator (Ternary operator)
if (x > 0)
y = 1;
else
y = -1;
is equivalent to
Syntax:
result = (condition) ? Expression1 : Expression2;
08/10/2024 33
Conditional Operator, examples
08/10/2024 34
Nested Ternary Operator
Just like if…, ternary operator can be nested too
Syntax:
result = (condition) ? Expression1 : Expression2;
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
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
int n = -29;
int temp = (n<0)?-n:n;
cout<<temp;
08/10/2024 39
Exercise-3
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;
08/10/2024 41
Exercise-5
if(y/x > 2)
if(y % x !=2)
x = x + 2;
cout<<x<<”\n“<<y;
08/10/2024 42
Exercise-6
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.
08/10/2024 45