Comprehensive Guide to C++ Control Structures, Boolean Logic, and Decision Structures

Control Structures and Boolean Foundations

  • Control structures alter the execution path of a program by determining whether specific continuous blocks of code are executed or skipped.

  • Jumping capabilities in basic control flow:

    • Forward jumping (jumping down): Allows execution to skip a continuous segment of lines forward.

    • Backward jumping (jumping up): Cannot be performed using basic decision structures; moving execution backwards to repeat code requires loops or functions.

  • A Boolean expression is any expression that evaluates to a Boolean value.

  • Boolean values are named after the English logician and mathematician George Boole.

  • There are exactly two Boolean values in computer science and C++:

    • true (represented numerically as 1 in C++ outputs).

    • false (represented numerically as 0 in C++ outputs).

  • In C++, Boolean variables are declared using the bool data type and initialized to true or false.

  • Relational operators compare two operands and evaluate to a Boolean value:

    • Equality operator: ==

    • Inequality operator: !=

    • Greater than: >

    • Greater than or equal to: >=

    • Less than: <

    • Less than or equal to: <=

  • Distinction between assignment and equality testing in C++:

    • A single equal sign = is the assignment operator, used to store a value into a variable.

    • Attempting an expression like 2 = 2 results in a compilation error because literals cannot be assigned values and variable names cannot begin with a numeric digit.

    • A double equal sign == must be used to test equality between expressions.

  • Evaluation examples in C++:

    • Evaluating 3 > 2 returns 1 (true).

    • Evaluating 3 <= 2 returns 0 (false).

    • Evaluating 5 == 5.0 returns 1 (true). C++ compares underlying mathematical values rather than strict types, performing automatic type coercion/juggling.

    • Evaluating 5 != 5 returns 0 (false) because the two values are equal.

Lexicographical Ordering and ASCII Character Evaluation

  • Character comparison in C++ uses dictionary order (lexicographical ordering), which relies on underlying ASCII numeric values.

  • Alphabetical ordering rules:

    • Characters appearing earlier in the alphabet (e.g., 'A') have smaller ASCII values.

    • Characters appearing later in the alphabet (e.g., 'Z') have larger ASCII values.

  • ASCII specifics:

    • The uppercase character 'A' has an ASCII value of 6565.

    • Uppercase 'J' appears after 'A' in the character set, giving 'J' a strictly larger ASCII value than 6565.

    • Evaluating relational comparisons on characters (such as checking if 'J' > 'A') evaluates to true (1).

Logical Operators and Compound Boolean Expressions

  • Logical operators combine smaller Boolean expressions into compound Boolean expressions.

  • Three fundamental logical operators in C++:

    • Logical AND (&&): Represented by double ampersands. Evaluates to true if and only if both expressions evaluate to true.

    • Logical OR (||): Represented by double vertical pipes. Evaluates to true if at least one expression evaluates to true.

    • Logical NOT (!): Represented by an exclamation point. Inverts a Boolean value (transforms true to false, and false to true).

  • Permutations of two Boolean variables (aa and bb):

    • With 22 binary variables, there are 22=42^2 = 4 possible input outcome combinations: (T,T)(T, T), (T,F)(T, F), (F,T)(F, T), and (F,F)(F, F).

    • Converting true to 11 and false to 00 forms binary bit strings representing states.

    • For 22 binary variables with 44 rows in a truth table, there are 24=162^4 = 16 distinct possible Boolean functions.

  • Step-by-step evaluation of compound expression (a AND b) OR NOT(a OR b)(a \text{ AND } b) \text{ OR } \text{NOT}(a \text{ OR } b), written as (a×b)+¬(a+b)(a \times b) + \neg(a + b):

    • Inputs a=truea = \text{true}, b=trueb = \text{true}:

    • (a AND b)=true(a \text{ AND } b) = \text{true}

    • (a OR b)=true→NOT(a OR b)=false(a \text{ OR } b) = \text{true} \rightarrow \text{NOT}(a \text{ OR } b) = \text{false}

    • Evaluation: true OR false=true\text{true} \text{ OR } \text{false} = \text{true}

    • Inputs a=truea = \text{true}, b=falseb = \text{false}:

    • (a AND b)=false(a \text{ AND } b) = \text{false}

    • (a OR b)=true→NOT(a OR b)=false(a \text{ OR } b) = \text{true} \rightarrow \text{NOT}(a \text{ OR } b) = \text{false}

    • Evaluation: false OR false=false\text{false} \text{ OR } \text{false} = \text{false}

    • Inputs a=falsea = \text{false}, b=trueb = \text{true}:

    • (a AND b)=false(a \text{ AND } b) = \text{false}

    • (a OR b)=true→NOT(a OR b)=false(a \text{ OR } b) = \text{true} \rightarrow \text{NOT}(a \text{ OR } b) = \text{false}

    • Evaluation: false OR false=false\text{false} \text{ OR } \text{false} = \text{false}

    • Inputs a=falsea = \text{false}, b=falseb = \text{false}:

    • (a AND b)=false(a \text{ AND } b) = \text{false}

    • (a OR b)=false→NOT(a OR b)=true(a \text{ OR } b) = \text{false} \rightarrow \text{NOT}(a \text{ OR } b) = \text{true}

    • Evaluation: false OR true=true\text{false} \text{ OR } \text{true} = \text{true}

Short-Circuit Evaluation

  • Short-circuit evaluation is an execution optimization where the compiler skips evaluating the second expression if the first expression determines the overall result.

  • Logical AND short-circuiting:

    • Expression structure: expression1 AND expression2\text{expression}_1 \text{ AND } \text{expression}_2

    • If expression1\text{expression}_1 evaluates to false, the entire expression MUST be false. expression2\text{expression}_2 is skipped completely.

  • Logical OR short-circuiting:

    • Expression structure: expression1 OR expression2\text{expression}_1 \text{ OR } \text{expression}_2

    • If expression1\text{expression}_1 evaluates to true, the entire expression MUST be true. expression2\text{expression}_2 is skipped completely.

  • Analogy:

    • Given the disjunction: "I have five fingers on my left hand OR I fight sharks for fun."

    • Because the first statement ("I have five fingers on my left hand") is true, the entire OR statement is immediately true without needing to verify or evaluate the second statement.

Single Alternative Decision Structures

  • Decision structure execution cadence: "if [some condition] then [do action]".

  • Flowchart convention for decision structures:

    • Start/Begin node indicates the beginning of execution.

    • Diamond symbols represent decision points containing a Boolean expression condition.

    • Branch paths exit the diamond based on whether the condition evaluates to true (Yes) or false (No).

    • An indented code block inside curly brackets {} contains the action performed when the condition is true.

  • Characteristics of single alternative structures (if statements):

    • Provides a single optional path of execution.

    • If the condition is true, execution enters the conditional block before returning to the main program flow.

    • If the condition is false, execution skips the conditional block entirely.

    • Code located outside the conditional block is executed in all cases.

  • Code structure example:

    • Variable declaration: int num;

    • User input prompt and retrieval.

    • Check condition: if (num == 7)

    • Action inside block: Print output confirming "you typed in 7".

    • Subsequent program execution continues outside the block regardless of input.

  • Comparison to sorting logic without loops:

    • Manual comparison of multiple entities (e.g., checking if team 1 is better than team 2, team 3, and team 4) mimics the comparative steps of selection sort, but standard algorithmic iteration requires loops.

Dual Alternative Decision Structures

  • Dual alternative decision structures (if-else statements) provide two mutually exclusive execution paths.

  • Syntax rules:

    • The if keyword requires an explicit Boolean condition.

    • The else keyword does NOT take any condition.

    • The else block executes if and only if the associated if condition evaluates to false.

  • Logic flow:

    • Execution splits into two distinct paths.

    • Exactly one path is executed, and the other path is skipped; both paths can never execute together.

  • Parity determination example (Even vs. Odd using modulo arithmetic):

    • Read an integer num from input.

    • Evaluate expression: if (num % 2 == 0)

    • Modulo operation % computes the remainder of integer division by 22.

    • If remainder is 00: Executed if body outputs that the number is even.

    • else: Output that the number is odd.

    • Splitting logic guarantees complete coverage since an integer is strictly either even or odd.