4/9/2025
Computer Engineering Department
WEEK NO:03
Simple Flow of Control
1
4/9/2025
Overview
2.1 Variables and Assignments
2.2 Input and Output
2.3 Data Types and Expressions
2.4 Simple Flow of Control
2.5 Program Style
Simple Flow of Control
Flow of control
The order in which statements are executed
Branch
Lets program choose between two alternatives
2
4/9/2025
Branch Example
To calculate hourly wages there are two choices
Regular time ( up to 40 hours)
gross_pay = rate * hours;
Overtime ( over 40 hours)
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
The program must choose which of these
expressions to use
Designing the Branch
Decide if (hours >40) is true
If it is true, then use
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
If it is not true, then use
gross_pay = rate * hours;
3
4/9/2025
Implementing the Branch
if-else statement is used in C++ to perform a
branch
if (hours > 40)
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
else
gross_pay = rate * hours;
Implementing the Branch
4
4/9/2025
Boolean Expressions
Boolean expressions are expressions that are
either true or false.
comparison operators such as '>' (greater than)
are used to compare variables and/or numbers
(hours > 40) Including the parentheses, is the
boolean expression from the wages example
A few of the comparison operators that use two
symbols (No spaces allowed between the symbols!)
>= greater than or equal to
!= not equal or inequality
= = equal or equivalent
9
if-else Flow Control
if (boolean expression)
true statement
else
false statement
When the boolean expression is true
Only the true statement is executed
When the boolean expression is false
Only the false statement is executed
10
5
4/9/2025
if-else Flow Control
if (boolean expression)
{
true statements
}
else
{
false statements
}
When the boolean expression is true
Only the true statements enclosed in { } are executed
When the boolean expression is false
Only the false statements enclosed in { } are executed
11
AND
Boolean expressions can be combined into
more complex expressions with
&& -- The AND operator
True if both expressions are true
Syntax: (Comparison_1) && (Comparison_2)
Example: if ( (2 < x) && (x < 7) )
True only if x is between 2 and 7
Inside parentheses are optional but enhance meaning
12
6
4/9/2025
OR
| | -- The OR operator (no space!)
True if either or both expressions are true
Syntax: (Comparison_1) | | (Comparison_2)
Example: if ( ( x = = 1) | | ( x = = y) )
True if x contains 1
True if x contains the same value as y
True if both comparisons are true
13
NOT
! -- negates any boolean expression
!( x < y)
True if x is NOT less than y
!(x = = y)
True if x is NOT equal to y
! Operator can make expressions difficult to
understand…use only when appropriate
14
7
4/9/2025
Inequalities
Be careful translating inequalities to C++
if x < y < z translates as
if ( ( x < y ) && ( y < z ) )
NOT
if ( x < y < z )
15
Pitfall: Using = or ==
' = ' is the assignment operator
Used to assign values to variables
Example: x = 3;
'= = ' is the equality operator
Used to compare values
Example: if ( x == 3)
The compiler will accept this error:
if (x = 3)
but stores 3 in x instead of comparing x and 3
Since the result is 3 (non-zero), the expression is true
16
8
4/9/2025
Compound Statements
A compound statement is more than one
statement enclosed in { }
Branches of if-else statements often need to
execute more that one statement
Example: if (boolean expression)
{
true statements
}
else
{
false statements
}
17
Compound Statements
A compound statement is more than
one
statement enclosed in { }
Branches of if-else statements often
need to
execute more that one statement
Example:
if (boolean expression)
{
true statements
}
else
{
false statements
}
18
9
4/9/2025
Program Style
Items considered a group should look like
A program written with a
attention to style group
is easier to read Skip lines between logical groups of
easier to correct
statements
Indent statements within statements
easier to change
if (x = = 0)
statement;
Braces {} create groups
Indent within braces to make the group
clear
Braces placed on separate lines are
easier to locate 19
Truth tables
Precedence rules
20
10
4/9/2025
Truth tables
Precedence rules
21
C++ Operators Precedence Table
22
11
4/9/2025
C++ Operators Precedence Table
23
C++ Operators Precedence Table
24
12
4/9/2025
C++ Operators Precedence Table
25
Simple Flow of Control
• Flow of control
• The order in which statements are executed • Designing the Branch
• Branch • Decide if (hours >40) is true
• Lets program choose between two alternatives
• If it is true, then use gross_pay =
Branch Example rate * 40 + 1.5 * rate * (hours - 40);
• To calculate hourly wages there are
two choices • If it is not true, then use
• Regular time ( up to 40 hours) gross_pay = rate * hours;
• gross_pay = rate * hours;
• Overtime ( over 40 hours)
• gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
• The program must choose which of these
expressions to use
26
13
4/9/2025
Implementing the Branch
• if-else statement is used in C++ to perform a
branch
if (hours > 40)
gross_pay = rate * 40 + 1.5 * rate * (hours - 40);
else
gross_pay = rate * hours;
27
Implementing the Branch
28
14
4/9/2025
if-else Flow Control
• if (boolean expression) • if (boolean expression)
true statement {
else true statements
false statement }
else
• When the boolean expression is {
true false statements
• Only the true statement is executed }
• When the boolean expression is • When the boolean expression is true
false • Only the true statements enclosed in { } are
• Only the false statement is executed executed
• When the boolean expression is false
• Only the false statements enclosed in { }
are executed 29
if-else Flow Control
• if (boolean expression) • if (boolean expression)
true statement {
else true statements
false statement }
else
• When the boolean expression is {
true false statements
• Only the true statement is executed }
• When the boolean expression is • When the boolean expression is true
false • Only the true statements enclosed in { } are
• Only the false statement is executed executed
• When the boolean expression is false
• Only the false statements enclosed in { }
are executed 30
15
4/9/2025
Flow of Control
Another way to say: The order in Implementing IF/ELSE Statements in C++
which statements get executed
• Branch: (verb) How a program
chooses between 2 alternatives.
• Usual way is by using an if-else
statement
if (Boolean expression)
true statement
else
false statement
31
IF/ELSE in C++
32
16
4/9/2025
Examples of IF Statements
33
Multiway Branching
34
17
4/9/2025
Defaults in Nested IF/ELSE Statements
35
Defaults in Nested IF/ELSE Statements
36
18