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 as1in C++ outputs).false(represented numerically as0in C++ outputs).
In C++, Boolean variables are declared using the
booldata type and initialized totrueorfalse.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 = 2results 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 > 2returns1(true).Evaluating
3 <= 2returns0(false).Evaluating
5 == 5.0returns1(true). C++ compares underlying mathematical values rather than strict types, performing automatic type coercion/juggling.Evaluating
5 != 5returns0(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 .Uppercase
'J'appears after'A'in the character set, giving'J'a strictly larger ASCII value than .Evaluating relational comparisons on characters (such as checking if
'J' > 'A') evaluates totrue(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 totrueif and only if both expressions evaluate totrue.Logical OR (
||): Represented by double vertical pipes. Evaluates totrueif at least one expression evaluates totrue.Logical NOT (
!): Represented by an exclamation point. Inverts a Boolean value (transformstruetofalse, andfalsetotrue).
Permutations of two Boolean variables ( and ):
With binary variables, there are possible input outcome combinations: , , , and .
Converting
trueto andfalseto forms binary bit strings representing states.For binary variables with rows in a truth table, there are distinct possible Boolean functions.
Step-by-step evaluation of compound expression , written as :
Inputs , :
Evaluation:
Inputs , :
Evaluation:
Inputs , :
Evaluation:
Inputs , :
Evaluation:
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:
If evaluates to
false, the entire expression MUST befalse. is skipped completely.
Logical OR short-circuiting:
Expression structure:
If evaluates to
true, the entire expression MUST betrue. 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 immediatelytruewithout 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) orfalse(No).An indented code block inside curly brackets
{}contains the action performed when the condition istrue.
Characteristics of single alternative structures (
ifstatements):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-elsestatements) provide two mutually exclusive execution paths.Syntax rules:
The
ifkeyword requires an explicit Boolean condition.The
elsekeyword does NOT take any condition.The
elseblock executes if and only if the associatedifcondition evaluates tofalse.
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
numfrom input.Evaluate expression:
if (num % 2 == 0)Modulo operation
%computes the remainder of integer division by .If remainder is : Executed
ifbody 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.