Program Control Structures: Conditional Control Structures

Program Control Structures

  • Flow of Control: Defines the order in which statements are executed during program runtime.
  • Sequential Flow: The default execution mode where instructions execute consecutively in the order written.
  • Selection: Alters execution path by choosing specific statements based on whether a condition evaluates to true or false.
  • Repetition: Repeats the execution of a block of statements based on loop conditions.
  • Invocation: Uses a single statement to call and execute an external block of instructions, such as a function.

Relational Operators and Expressions

  • Relational Expression (Condition): An expression comparing two operands using a relational operator, evaluating to boolean true (11 or any non-zero value) or false (00).
  • Relational Operators:
    • <: Less than
    • >: Greater than
    • <=: Less than or equal to
    • >=: Greater than or equal to
    • ==: Equal to
    • !=: Not equal to
  • Character Comparisons: Characters are evaluated using their underlying numeric ASCII decimal values (for example, 'B' < 'a' evaluates to true / 11 because ASCII 'B' is 6666 and 'a' is 9797).

ASCII Decimal Code Table

Logical Operators

  • Complex Conditions: Combine or invert relational expressions using boolean logic:
    • Logical AND (&&): Evaluates to true (11) only if both operands are true; otherwise evaluates to false (00).
    • Logical OR (||): Evaluates to true (11) if at least one operand is true; evaluates to false (00) only if both operands are false.
    • Logical NOT (!): Unary operator that reverses the truth value of an expression (transforms true to false and false to true).

Conditional Selection Statements

  • Single if Statement:
    • Executes a single statement or compound block enclosed in braces { } if the condition evaluates to true.
    • Bypasses the controlled block entirely if the condition evaluates to false (00).
  • if-else Statement:
    • Provides a two-way branch: executes the first statement/block if true, or the else block if false.

If-Else Statement Flowchart

  • if-else if Ladder:
    • Evaluated sequentially from top to bottom.
    • Once any condition evaluates to true, its corresponding statement executes and all remaining ladder branches are bypassed.
    • If none of the conditions evaluate to true, the trailing else statement executes.
  • Nested if Statements:
    • Placement of an if or if-else block inside another if or else branch.
    • Dangling Else Pairing Rule: By default, the compiler pairs an else with the closest preceding unpaired if, unless explicit curly braces { } dictate otherwise.

The Switch Statement

  • Multi-Way Equality Branching: Evaluates an integral or character expression against a series of defined case constant labels.
  • Data Type Restrictions:
    • Allowed: Integers (int) and individual characters (char).
    • Prohibited: Floating-point types (float, double) and strings/words.
  • Control Flow Elements:
    • break: Terminates the switch construct; omitting break causes unintentional fall-through execution into subsequent cases.
    • default: An optional branch that executes if the evaluated expression does not match any specified case constant.

Review Questions & Conceptual Checks

  • Divisibility Determination: The modulus operator (%) checks integer divisibility (for example, no % 2 == 0 evaluates whether an integer is even).
  • Switch Fall-Through: Removing a break statement causes execution to cascade directly into subsequent case blocks regardless of their match status.
  • Omitting Default: If no matching case exists and default is absent, the entire switch statement exits without executing any branch.
  • Case Syntax: Removing single quotation marks around a character label (e.g., using case s: instead of case 's':) causes a syntax/compilation error as the compiler attempts to resolve an undeclared identifier.