Control Flow recap

Congratulations! You've learned how to control the flow of program execution, create loops, and use control flow statements effectively. Let's review the key concepts from this section.

Execution Path and Control Flow

The sequence of instructions executed by the CPU is called the program's execution path. Programs that always take the same path are called straight-line programs.

Control flow statements (or flow control statements) allow programs to alter their execution path. When a program begins executing a non-sequential instruction, this is called a branch.

A conditional statement determines whether associated code should execute based on a condition's truth value.

Conditional Statements

If statements execute code when a condition is true. Else statements execute when the condition is false. Multiple if-else statements can be chained together.

A dangling else happens when it's ambiguous which if-statement an else-statement belongs to. C++ matches else with the nearest unmatched if in the same block. Always use braces to avoid ambiguity.

A null statement contains only a semicolon and does nothing. It's used when syntax requires a statement but no action is needed.

Switch Statements

Switch statements provide an efficient way to select among multiple options. They work only with integral types. Case labels specify values to match against. The default label handles values with no matching case.

Fallthrough occurs when execution continues from one case into the next. Use break (or return) to prevent fallthrough. The [[fallthrough]] attribute documents intentional fallthrough.

Goto Statements

Goto statements jump to labeled locations in code. They should be avoided as they create spaghetti code - code with tangled execution paths that's difficult to understand.

Loops

While loops repeatedly execute code while a condition is true. The condition is checked before each iteration.

An infinite loop has a condition that never becomes false, causing the loop to run forever unless stopped by other control flow statements.

A loop variable (or counter) tracks how many times a loop has executed. Each loop execution is called an iteration.

Do-while loops are similar to while loops but check the condition after executing, guaranteeing at least one execution.

For loops are ideal when iterating a specific number of times. An off-by-one error occurs when a loop iterates too many or too few times.

Loop Control Statements

Break statements exit from loops (while, do-while, for, range-based for) and switch statements. Continue statements skip to the next loop iteration.

Program Termination

Halts terminate program execution. Normal termination means the program ended as expected (the status code indicates success or failure). std::exit() is called automatically at main's end or can be called explicitly. It performs cleanup but doesn't clean up local variables or unwind the call stack.

Abnormal termination happens when the program encounters an unexpected error requiring shutdown. std::abort() performs abnormal termination.

Algorithms and State

An algorithm is a finite sequence of instructions that solves a problem or produces a result. A stateful algorithm retains information between calls. A stateless algorithm needs all information provided each call. The term state refers to the current values held in stateful variables.

A deterministic algorithm always produces the same output sequence for a given input.

Random Number Generation

A pseudo-random number generator (PRNG) is an algorithm generating number sequences that simulate randomness. PRNGs are initialized with a random seed (or seed). A seeded PRNG is one that has been initialized. When the seed value is smaller than the PRNG's state, the PRNG is underseeded. The sequence length before a PRNG repeats is its period.

A random number distribution transforms PRNG output into a different distribution. A uniform distribution generates numbers between two values with equal probability.

Key Terminology

  • Execution path: The sequence of instructions executed by the CPU
  • Straight-line program: A program that always takes the same execution path
  • Control flow statement: Statement that alters a program's execution path
  • Branch: When a program begins executing a non-sequential instruction
  • Conditional statement: Statement that determines whether code should execute based on a condition
  • If statement: Statement that executes code when a condition is true
  • Dangling else: Ambiguity about which if-statement an else belongs to
  • Null statement: A statement containing only a semicolon that does nothing
  • Switch statement: Statement for selecting among multiple options based on an integral value
  • Case label: Specifies a value to match in a switch statement
  • Default label: Handles values with no matching case in a switch
  • Fallthrough: When execution continues from one case into the next
  • Goto statement: Statement that jumps to a labeled location (avoid using)
  • Spaghetti code: Code with tangled execution paths that is difficult to understand
  • While loop: Loop that executes while a condition is true
  • Infinite loop: Loop with a condition that never becomes false
  • Loop variable/Counter: Variable tracking how many times a loop has executed
  • Iteration: A single execution of a loop body
  • Do-while loop: Loop that checks condition after executing
  • For loop: Loop ideal for iterating a specific number of times
  • Off-by-one error: Error when a loop iterates too many or too few times
  • Break statement: Statement that exits from loops and switch statements
  • Continue statement: Statement that skips to the next loop iteration
  • Halt: Statement that terminates program execution
  • Normal termination: Program ended as expected
  • Abnormal termination: Program ended due to unexpected error
  • Algorithm: Finite sequence of instructions that solves a problem
  • Stateful algorithm: Algorithm that retains information between calls
  • Stateless algorithm: Algorithm that needs all information provided each call
  • Pseudo-random number generator (PRNG): Algorithm generating sequences that simulate randomness
  • Random seed/Seed: Initial value used to initialize a PRNG
  • Period: Sequence length before a PRNG repeats
  • Random number distribution: Transforms PRNG output into a different distribution
  • Uniform distribution: Distribution generating numbers with equal probability

Looking Forward

Control flow is fundamental to programming. The concepts you've learned in this section—conditional statements, loops, and control flow—form the building blocks for writing complex programs. Practice using these tools to develop logical thinking and problem-solving skills that will serve you throughout your programming journey.