Program Control Structures: Conditional Control Structure
Program Flow of Control Structures
- Flow of Control: The sequential order in which a program's statements are executed.
- Sequential Execution: The standard default flow where statements execute line by line in order.
- Selection: Control structure used to direct execution down alternative paths based on whether a condition evaluates to true or false.
- Repetition: Control structure used to repeatedly execute a block of statements while a condition remains satisfied.
- Invocation: Control structure used to call a sequence of instructions using a single statement, such as a function call.
Relational Operators & Expressions
- Relational Expressions: Also known as conditions; compare two operands and evaluate to either true (non-zero, typically 1) or false (0).
- Relational Operators:
< (Less than)> (Greater than)<= (Less than or equal to)>= (Greater than or equal to)== (Equal to)!= (Not equal to)
- Character Data Comparison: Characters are evaluated using their ASCII decimal codes (e.g.,
'A' = 65, 'a' = 97, 'Z' = 90; 'B' < 'a' evaluates to 1 / true, whereas 'A' > 'C' evaluates to 0 / false).
Logical Operators & Expressions
- Logical Expressions: Combine or invert relational expressions to construct complex conditions.
- Logical Operators:
- Logical AND (
&&): Evaluates to true only if both operands evaluate to true. - Logical OR (
||): Evaluates to true if at least one operand is true; evaluates to false only when both operands evaluate to false. - Logical NOT (
!): Unary operator that reverses the truth value of its operand (!true is false, !false is true).
Selection Control Structures
- Single
if Statement: Executes a statement or a compound statement block enclosed in braces {} only if the evaluated condition is true.

if-else Statement: Provides a two-way branch; executes the true branch statement when the condition is non-zero, or the else branch statement when the condition evaluates to 0.

if-ladder (else if): Evaluates multiple conditions sequentially from the top down. As soon as a condition evaluates to true, its statement block executes and the rest of the ladder is bypassed. The optional trailing else statement executes only if all preceding conditions evaluate to false.- Dangling Else Pairing Rule: The compiler associates an
else statement with the closest previous unpaired if statement, unless braces {} are explicitly used to change the grouping. - Nested
if Statements: Consists of one or more if or if-else structures enclosed within another if or else block to handle multi-tiered conditional checks.
The switch Statement
- Multi-way Equality Testing: Compares an expression for exact equality against a predefined list of integral case values (
case constant:). - Data Type Restrictions: Floating-point values (
float, double) are invalid in switch statements; selector expressions and case constants must be integral types (such as int or char). - Case Constants: Must be literal constants or constant expressions evaluated to an integer; variable identifiers are not permitted.
- The
break Statement: Terminates execution inside the switch block and exits; omitting break causes execution to fall through into subsequent cases regardless of whether those case labels match. - Case Stacking: Multiple
case labels can be stacked sequentially without a break to execute the same statement block for multiple inputs (e.g., uppercase and lowercase characters). - The
default Label: An optional catch-all clause that executes when none of the defined case constants match the selector expression.
Questions & Discussion
- What if
break is removed on the first case? Control falls through directly into the statements of the subsequent case, executing them regardless of matching conditions until a break statement or the end of the switch block is encountered. - What if
default is removed from the program? If the input does not match any specified case constant, no action occurs inside the switch block, and the program bypasses it to execute the subsequent code. - What if the single quotation mark is removed in a character case constant? The compiler treats the character as a variable identifier rather than a character literal constant, resulting in a syntax/compilation error if undefined or non-constant.
- What if a
break statement is added after each case of a capital letter? It halts fall-through, preventing shared statement execution between uppercase and lowercase character pairs.