This document discusses control structures in C++ programming, including if, if/else, and if/else if statements. It explains how to use logical operators like &&, ||, and ! to create more complex conditional expressions. Examples are provided to demonstrate simple if statements, if/else statements, nested if statements, and if/else if statements. The key points covered are how to make decisions in code using conditional statements, the logical flow of if statements, and how to combine conditions using logical operators.
2. OBJECTIVES
• In this Lecture you will learn how to:
– Make decisions
• Using :
– simple if
– If…else statement
– If…else if statement
– Use logical operators
2
Intro to Programming with C++
Sir Noble
3. Program Control and Control Structures
– An algorithm is a procedure for solving a
problem in terms of
• the actions to execute and
• the order in which the actions execute
– Program control
• Specifying the order in which statements (actions)
execute.
– Control Structures
• Simple control statements
3
Intro to Programming with C++
Sir Noble
4. The Simple if Statement
• Allows statements to be conditionally executed or
skipped over
• Models the way we mentally evaluate situations:
– "If I do not have the minimum CGPA required, I will not
graduate."
– "If I have codeblocks installed on my machine, I will be able
to write and debug simple c++ programs."
• If condition is true, body of the if statement
executes
• If condition is false, body of the if statement does
not execute
4
Intro to Programming with C++
Sir Noble
5. The Logical flow of Simple If
Decision
5
Intro to Programming with C++
Sir Noble
6. Decision Making: Equality and Relational
Operators
• The if Statement
– Syntax
– Boolean expression is any C++
expression that can be evaluated
– Statement is any C++ statement or block
of statements that you want to execute
when the boolean expression evaluates
as true.
6
if ( boolean expression )
Statement;
Intro to Programming with C++
Sir Noble
10. Expanding the if Statement
• To execute more than one statement as part of an if
statement, enclose them in curly braces{ }:
if (gpa >= 3.75)
{
cout << “ GPA is ”<< << gpa << endl;
cout <<“Congrats! You are a first class
student” << endl;
}
• { } creates a block of code
10
Intro to Programming with C++
Sir Noble
11. Nested if Statements
• An if statement that is nested inside another
if statement
• Nested if statements can be used to test more
than one condition
11
Intro to Programming with C++
Sir Noble
13. The if/else statement
• Provides two possible paths of execution
• Performs one statement or block if the
expression is true, otherwise performs
another statement or block.
13
Intro to Programming with C++
Sir Noble
14. The if/else statement
• General Format:
if (expression)
statement1; // or block
else
statement2; // or block
14
True
False
Intro to Programming with C++
Sir Noble
15. if/else-What Happens
To evaluate:
if (expression)
statement1;
else
statement2;
• If the expression is true, then statement1 is executed
and statement2 is skipped.
• If the expression is false, then statement1 is skipped
and statement2 is executed.
• For example, consider this pseudocode
if student’s grade is greater than or equal to 40
Print “Passed”
else
Print “Failed”
15
Intro to Programming with C++
Sir Noble
16. 16
Example If - else Statement
Intro to Programming with C++
Sir Noble
17. Blocks
• Blocks/Compound Statement
– Set of statements within a pair of braces
– Used to include multiple statements in an if body
– Example
– Without braces
cout << "You must take this course again" << endl;
always executes
17
if ( studentGrade >= 60 )
cout << "Passed“ << endl;
else
{
cout << "Failed“ << endl;
cout << "You must take this course
again“ << endl;
}
Intro to Programming with C++
Sir Noble
18. The if/else if Statement
• Tests a series of conditions until one is found to
be true
• Often simpler than using nested if/else
statements
• Can be used to model thought processes such as:
If I have ‘chaw’ money, I will buy pizza,
else, if it is not ‘chaw’, I will buy kenkey and fish,
else, no ‘chop chop’
18
Intro to Programming with C++
Sir Noble
20. The if/else if Statement in
Program
20
Intro to Programming with C++
Sir Noble
21. Logical Operators
• Logical operators
– Allows for more complex conditions
• Combines simple conditions into complex conditions
• Used to create relational expressions from other relational
expressions
• C++ logical operators
– && (logical AND)
– || (logical OR)
– ! (logical NOT)
21
Intro to Programming with C++
Sir Noble
22. Logical Operators
• Logical AND (&&) Operator
– It takes two expressions as operands and creates an
expression that is true only when both sub expressions
are true.
– Consider the following if statement
if ( temperature < 20 && minutes > 12 )
cout << “The temperature is in the danger
zone” << endl;
• Combined condition is true
– If and only if both simple conditions are true
• Combined condition is false
– If either or both of the simple conditions are false
22
Intro to Programming with C++
Sir Noble
23. 23
You must provide complete expressions on both sides of the &&
operator. For example, the following is not correct because the condition
on the right side of the && operator is not a complete expression.
temperature > 0 && < 100
The expression must be rewritten as
temperature > 0 && temperature < 100
Intro to Programming with C++
Sir Noble
24. Logical Operators
24
expression1 expression2 expression1 && expression2
false false false
false true false
true false false
true true true
Intro to Programming with C++
Sir Noble
25. Logical Operators
• Logical OR (||) Operator
– It takes two expressions as operands and creates an
expression that is true when either of the sub-
expressions are true.
– Consider the following if statement
if ( temperature < 20 || temperature > 100 )
cout << “The temperature is in the danger zone” << endl;
• Combined condition is true
– If either or both of the simple conditions are true
• Combined condition is false
– If both of the simple conditions are false
25
Intro to Programming with C++
Sir Noble
26. Logical Operators
26
expression1 expression2 expression1 || expression2
false false false
false true true
true false true
true true true
Intro to Programming with C++
Sir Noble
27. The logical || Operator in
Program
27
Intro to Programming with C++
Sir Noble
28. The logical || Operator in
Program
28
Intro to Programming with C++
Sir Noble
29. Logical Operators
• Logical NOT (!) Operator
– It takes an operand and reverses its truth or
falsehood.
– If the expression is true, the ! Operator returns false
and if the expression is false, it returns true
– Consider the following if statement
if ( !(temperature > 100) )
cout << “You are below the maximum temperature” << endl;
29
Intro to Programming with C++
Sir Noble
30. Logical Operators-Examples
int x = 5, y = -7, z = 9;
(x > y) && (y > z) True/False
(x > y) && (z > y) True/False
(x <= z) || (y == z) True/False
(x <= z) || (y != z) True/False
!(x >= z) True/False
30
Intro to Programming with C++
Sir Noble
31. Exercise – if-else…if-else
31
• A program that calculates the charges for
membership in a Sanhedrin Gym. The gym has three
membership packages to choose from: bronze
membership, silver membership and gold
membership. The program presents a menu that
allows the user to choose the desired package and
then calculates the cost of the membership.
Membership rates
Bronze membership: GHC 30.00 per month
Silver membership: GHC 40.00 per month
Gold membership: GHC 50.00 per month
Intro to Programming with C++
Sir Noble
Hinweis der Redaktion
The trailing else clause is optional, but it is best used to catch errors.