Comprehensive Study Notes on Conditional Logic, Boolean Expressions, and Algorithmic Flow

Conditional Control Structures and Chained Decision Trees

  • Conditional execution relies on evaluating expressions to determine flow control within a program.
  • Two-Stage Evaluation Pattern:
    • Initial state criteria (e.g., verifying state residency) are evaluated prior to nested conditional checks (e.g., verifying age thresholds).
    • In string comparison, equality comparison syntax requires double equal signs (==). Single equal signs (=) represent variable assignment.
    • Variable comparison with string literals is case-sensitive. Matching fails if cases differ between the variable contents and the string literal.
  • Multi-Branch Conditional Chains (if / else if / else):
    • Conditions are evaluated sequentially from top to bottom.
    • Once a condition evaluates to true, the code block bound to that condition executes, and the remainder of the conditional chain is skipped entirely.
    • Sequence thresholds for driver's licensing logic:
    • Condition 1: age≥18\text{age} \ge 18 →\rightarrow Output: Eligible for an Ohio hybrid driver's license.
    • Condition 2: age≥16\text{age} \ge 16 →\rightarrow Output: Eligible for an Ohio restricted driver's license.
    • Condition 3: age≥15.5\text{age} \ge 15.5 →\rightarrow Output: Eligible for the Ohio learner's permit program.
    • Default Condition (else): Output: No license or permit permitted.
  • Implicit Range Checking:
    • Testing discrete ranges (e.g., explicitly checking 16≤age<1816 \le \text{age} < 18) is unnecessary when higher values are checked first.
    • Because the initial check tests if age≥18\text{age} \ge 18, reaching the second check guarantees that age<18\text{age} < 18. Thus, testing age≥16\text{age} \ge 16 implicitly evaluates the range 16≤age<1816 \le \text{age} < 18.
    • Boundaries without strict upper constraints hypothetically permit extreme values (e.g., an upper toy age limit of 9999 years, where a age of 100100 violates the upper bound).
  • Flow Control Mechanics in Visual Flowcharts (e.g., Flowgorithm):
    • Main flow proceeds down single control paths.
    • Branching occurs at decision nodes: true branches execute dedicated output/processing blocks before rejoining the main flow, while false branches point directly into the next chained if statement.
    • The terminal else node contains no attached if condition and serves as the universal fallback.

Input Sanitization and the Garbage In, Garbage Out Principle

  • Software programs execute instructions literally without implicit contextual awareness.
  • Problem Formulation & Pseudocode Strategy:
    • Using dedicated outcome variables preserves state and permits a single output node at the end of execution, avoiding scattered output operations throughout conditional branches.
  • WiFi Signal Strength Evaluation Algorithm:
    • Threshold lookup table logic:
    • distance≤40\text{distance} \le 40: Signal strength is rated as strong.
    • distance≤80\text{distance} \le 80 (implicit range 40<distance≤8040 < \text{distance} \le 80): Signal strength is rated as medium.
    • distance≤120\text{distance} \le 120 (implicit range 80<distance≤12080 < \text{distance} \le 120): Signal strength is rated as weak.
    • Default (distance>120\text{distance} > 120): No signal available.
  • Flaws in Input Handling and Boundary Vulnerabilities:
    • Physical distance is scalar and measured in non-negative absolute values.
    • Without input verification, a negative input value (e.g., −5-5 or −2000-2000) incorrectly satisfies distance≤40\text{distance} \le 40, reporting a "strong signal" at a distance of −2000-2000 units.
  • Garbage In, Garbage Out (GIGO):
    • Unsanitized inputs yield meaningless outputs. Developers must explicitly sanitize and validate inputs prior to condition testing.
    • Hardware-level data types enforce signed versus unsigned constraints (where unsigned types permit non-negative values only), but higher-level logic must handle user validation manually.

Sequential Testing and Voltage Evaluation Logic

  • Battery Condition Tester Algorithm:
    • Logic must handle negative potential created when a battery is connected backwards.
    • Ordered sequence of non-overlapping tests:
    • Step 1: voltage<0 V\text{voltage} < 0\,\text{V} →\rightarrow Output: Battery is installed backwards in the tester.
    • Step 2: voltage<0.5 V\text{voltage} < 0.5\,\text{V} →\rightarrow Output: Replace battery (dead condition).
    • Step 3: voltage<1.0 V\text{voltage} < 1.0\,\text{V} (e.g., 0.7 V0.7\,\text{V}) →\rightarrow Output: Battery is in fair condition.
    • Step 4: Default (voltage≥1.0 V\text{voltage} \ge 1.0\,\text{V}) →\rightarrow Output: Battery is in good condition.
  • Structuring tests from error states to positive bounds converts complex tables into clean linear flows.

Complex Boolean Expressions and Evaluation Dynamics

  • Combining Logical Expressions:
    • Compound conditions utilize logical operators: AND (∧\land) and OR (∨\lor).
    • Operator Precedence Rules: Relational operators (==, !=, <, >, <=, >=) maintain higher precedence than logical operators (AND, OR).
    • Expressions are evaluated by processing high-precedence relational checks first, pushing results onto an execution stack, and joining results with low-precedence logical operators.
  • Practical Compound Logic Examples:
    • Senior Programmer Bonus Requirement:
    • Requirement: Employee must be a programmer and hold senior rank to receive a 10%10\% salary increase.
    • Expression: `if (jobCategory ==