Chapter 7: Advanced Control Flow

Chapter Goals

Multiple Alternatives

Multiple Alternatives (Coins Flowchart)

Multiple Alternatives (coins6.cpp)

Multiple Alternatives

Multiple Alternatives (richter.cpp)

Nested Branches

Nested Branches

Nested Branches (tax.cpp)

Boolean Operations

Boolean Operations

DeMorgan's Law(futval.cpp)

The for Loop

The for Loop (Syntax 7.1 : for Statement)

Syntax 7.1 : for Statement

for (initialization_expression; condition; update_expression) statement
Example:
for (int i = 1; i <= 10; i++) sum = sum + i;
Purpose: Execute the initialization statement. While the condition remains true, execute the statement and update the expression.

The for Loop (forfac.cpp)

The do Loop

The do Loop (Syntax 7.2 : do/while Statement)

Syntax 7.2: do/while Statement

do statement while (condition);
Example:
do x = sqrt(x) while (x >= 10);
Purpose: Execute the statement, then test the condition, and repeat the statement while the condition remains true.

The do Loop (sqroot.cpp)

Nested Loops

Nested Loops

Nested Loops(table.cpp)

Nested Loops

Processing Text Input

Simulations

Simulations

Simulations

Simulations (dice.cpp)

Simulations

Simulations (Buffon Needle Experiment)

Simulations (buffon.cpp)