Chapter 4
Chapter 4
Control Structures
Control Structures: Decisions
Control Structures are also called Selection or Decision making
Logical Operators
Logical Operators
The logical operators are used to combine two or more conditional
operators.
Conditional Meaning Description
AND :
operator T && T =T
&& AND Result is true only if the condition T && F =F
on both sides of the operator are F && T =F
F && F =F
true
OR :
|| OR Result is false only if the condition T || T = T
T || F = T
on both sides of the operator are
F || T = T
false F || F = F
! NOT Reverses logic of a
condition
Logical Operators
int x = 12, y = 5, z = -4;
The logical && operator in Program 4-15
The logical && operator in Program 4-16
The logical && operator in Program 4-17
Logical Operators
! has highest precedence, followed by &&, then ||
The if Statement
The If Statement
Allows statements to be conditionally executed or skipped over
Models the way we mentally evaluate situations:
"If it is raining, take an umbrella."
"If it is cold outside, wear a coat."
The general form of an if-statement is as follows;
if (conditional expression)
statement; // instruction to be executed if condition is true
If expression is true, then the statement is executed; otherwise,
statement is skipped and control passes to the next statements.
if Statement in Program 4-2
if Statement in Program 4-2
If Statement
Note:
Do not place ; after (expression)
The expression part must be always in parenthesis.
Place statement; on a separate line after (expression),
indented:
if (score > 90)
grade = 'A';
Be careful testing floats and doubles for equality
0 is false; any other value is true
Expanding the if Statement
To execute more than one statement as part of an if
statement, enclose them in { }:
if (score > 90)
{
grade = 'A';
cout << "Good Job!\n";
}
{ } creates a block of code
4 :4
General Format:
if (expression) Executed if
statement1; // or block.expression is true
else
Executed if
statement2; // or block
expression is
false
The if/else statement and Modulus Operator in Program 4-8
Program 4-9
Program 4-9
4:5
Nested if Statements
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
The general form of an nested if-statement is as follows;
if (expresion1)
if (expresion2)
statement;
If the expression1 of the first if is true, control passes to the next if. If
the expression2 of the second if is also true, statement will be
executed.
If one of the if-statements fail, execution continues from the
statements after the statement and statement is not executed.
Program 4-10
Use Proper Indentation!
4:6
if (expression)
statement1; // or block
else if (expression)
statement2; // or block
.
. // other else ifs .
else if (expression)
statementn; // or block
The if/else if Statement in Program 4-13
Using a Trailing else to Catch Errors in Program 4-14
• The trailing else clause is optional, but it is best used to catch
errors.
4:7
Flags
Flags
Variable that signals a condition
Usually implemented as a bool variable
Can also be an integer
The value 0 is considered false
Any nonzero value is considered true
As with other variables in functions, must be assigned an initial value
before it is used
4:8
Menus
Menu
Menu-driven program: program execution controlled by user selecting
from a list of actions
Menu: list of choices on the screen
Menus can be implemented using if/else if statements
Display list of numbered or lettered choices for actions
Prompt user to make selection
Test user selection in expression
if a match, then execute code for action
if not, then go on to next expression
4:9
1 Hamburger (40tl)
2 Chicken Wrap (35tl)
3 Cheese Burger (45tl)