Source: Computer Science I, University of Central Florida
Tags: control structures, if statements, boolean expressions, boolean logic, decision structures, if-else, nested if, else if, logical operators, AND OR NOT, case structure, switch statement, short circuit evaluation, comparison operators, Java conditionals
Difficulty: Beginner Prerequisites: Variables, data types, basic input/output (Modules 1–3).
Control structures are the building blocks that let a programme do more than run line by line. They allow branching (doing different things depending on a condition) and, eventually, looping. This module focuses on the branching side: how to make decisions in code. If you understand variables and basic expressions from earlier modules, you have everything you need here. Mastering these structures is essential because virtually every non-trivial programme uses them.
Every programme uses three control structures: sequence, decision, and repetition. This module covers decision structures, where your code checks a Boolean condition (true or false) and runs different blocks depending on the result. You will learn single-alternative (If), dual-alternative (If-Else), chained (If-Else If), nested, and case/switch structures, plus the logical operators (AND, OR, NOT) that let you combine conditions.
Control structure
The fundamental patterns that govern the order in which statements execute. The three types are sequence, decision, and repetition.
Sequence structure
Statements execute one after another, top to bottom, with no branching. Think of it as the default: if you write three lines, the computer runs all three in order.
Decision structure (selection structure)
A structure that evaluates a condition and chooses which block of code to run. In simple terms, this is how your programme "makes choices."
Repetition structure (loop)
A structure that repeats a block of code while a condition holds. Covered in detail in later modules, but listed here because it is one of the three fundamental control types.
Boolean expression
An expression that evaluates to either True or False (represented internally as 1 and 0). In simple terms, it is a yes-or-no question your code asks before deciding what to do.
Comparison operator (relational operator)
An operator that compares two values and returns a Boolean result. The standard set: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
Assignment operator (=)
Sets a variable to a value. Students frequently confuse this with the equality comparison operator (==).
Logical operator
An operator that combines or negates Boolean expressions: AND, OR, NOT. In Java these are written as &&, ||, and !.
Short-circuit evaluation
The behaviour where the computer stops evaluating a compound Boolean expression as soon as the overall result is determined. For AND, if the first operand is false the whole thing is false; for OR, if the first operand is true the whole thing is true. Placing the simpler (faster) condition first can save processing time.
Case structure (switch statement)
A cleaner alternative to a long chain of If-Else If blocks when you are comparing a single variable against several discrete values. Called switch in Java and most C-family languages.
Nested decision structure
An If statement placed inside another If statement, allowing multi-layered condition checking.
Sequence – statements run in order, top to bottom.
Decision – the programme evaluates a condition and branches accordingly.
Repetition – the programme repeats a block while a condition is true (covered later).
The simplest decision structure. If the condition is true, run the enclosed statements. If false, skip them entirely and continue after the block.
Pseudocode:
If condition Then
Statement
Statement
End If
Java equivalent:
if (condition) {
// statements
}
In a flowchart, a decision point is drawn as a diamond shape with two outgoing paths (true and false).
A Boolean expression always resolves to true or false. You build them with comparison operators:
== checks equality (comparing operator)
= assigns a value (assignment operator) – these two are not interchangeable
!= checks inequality (pronounced "bang equals")
>, <, >=, <= compare magnitude
Examples in pseudocode:
length > width – true when length is larger
area <= 20 – true when area is 20 or less
numInput == 0 – true when numInput is exactly zero
charRead == 'x' – compares a character variable to the character literal x (note single quotes for a single character)
xVal != 0 – true when xVal is anything other than zero
Provides two paths: one block runs when the condition is true, a different block runs when it is false.
Pseudocode:
If condition Then
Statement (true path)
Else
Statement (false path)
End If
Example with string comparison:
Declare String name1 = "Mary"
Declare String name2 = "Mark"
If name1 == name2 Then
Display "The names are the same"
Else
Display "The names are not the same!"
End If
Use == to compare strings for equality in pseudocode. (In Java, string comparison has its own nuances with .equals(), which you will encounter later.)
An If statement inside another If statement. Each level adds another layer of condition checking. Indentation is critical for readability.
If temp < 40 Then
Display "Cold, isn't it?"
Else
If temp < 90 Then
Display "Nice weather!"
Display "Time for a picnic"
Else
Display "It's hot!"
End If
End If
Notice that the inner If only runs when the outer condition is false (temp is 40 or above). The nesting means the programme narrows down through conditions step by step.
When you need to test a series of related conditions, an If-Then-Else If chain avoids deep nesting. A common use case is mapping numeric scores to letter grades.
If condition1 Then
...
Else If condition2 Then
...
Else
...
End If
The conditions are tested top to bottom. The first one that evaluates to true has its block executed; everything below is skipped. The trailing Else is the catch-all for when none of the conditions match.
Logical operators let you combine multiple Boolean expressions into a single compound expression.
AND
Both sides must be true for the whole expression to be true.
Pseudocode: length > width AND width != 0
Java: (length > width && width != 0)
OR
At least one side must be true for the whole expression to be true.
Pseudocode: length > width OR width > 0
Java: (length > width || width > 0)
NOT
Reverses the Boolean value of the expression it wraps.
NOT(2 > 3) evaluates to true (because 2 > 3 is false, and NOT flips it)
NOT(1 > 2) also evaluates to true (same reasoning: 1 > 2 is false)
Wait, correction: NOT(1 > 2) is true; but NOT(3 > 2) would be false. The NOT operator simply inverts whatever it receives.
Pseudocode: NOT(length > width)
Java: !(length > width)
When the computer evaluates a compound Boolean expression with AND or OR, it can sometimes skip the second operand entirely:
With AND, if the first operand is false, the result is false regardless, so the second operand is never checked.
With OR, if the first operand is true, the result is true regardless.
Practical tip: place the simpler or cheaper condition first. This saves processing time when the shortcut kicks in.
An alternative to long If-Else If chains when you are comparing one variable against several specific discrete values (typically integers or characters).
Pseudocode:
Select testExpression
Case value1
...
Case value2
...
Default:
...
End Select
The testExpression must be a discrete value, usually an integer or a character. The Default block handles any value not matched by the listed cases.
In Java, this is written as a switch statement.
A variable that stores only true or false. Useful as a flag to track whether something has happened.
Boolean b = FALSE
If b != TRUE Then
...
You can also use Boolean variables directly in conditions without comparing them to TRUE or FALSE, since they already are Boolean values: If NOT b Then ... is equivalent.
No mathematical formulas in this module. The key visual to know: in flowcharts, a decision point is drawn as a diamond, with one path for true and one for false.
Truth table summary for logical operators:
A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Decision structures are everywhere. A login system checks whether a username and password match before granting access. An e-commerce site uses If-Else If chains to apply different discount tiers based on order value. ATMs use case/switch logic to route your menu selection (withdraw, deposit, check balance) to the right operation.
Students often confuse = (assignment) with == (comparison). Writing if (x = 5) assigns 5 to x instead of checking whether x equals 5. In some languages this compiles without error, making the bug hard to spot.
Students sometimes assume that when an If condition is false, the programme stops entirely. It does not; execution simply skips the If block and continues with whatever comes after.
With AND and OR, students mix up which requires both conditions and which requires only one. Remember: AND is strict (both must be true), OR is lenient (one is enough).
In nested structures, students lose track of which End If closes which If. Consistent indentation prevents this.
⚠️ The difference between = and == is a near-guaranteed exam question. Know it cold.
⚠️ Be able to trace through nested If-Else structures and predict which branch executes for a given input value.
⚠️ Know the truth tables for AND, OR, and NOT. Exam questions often give a compound expression and ask you to evaluate it.
⚠️ Understand when to use a case/switch structure versus an If-Else If chain: case/switch works for discrete values, not ranges.
⚠️ Short-circuit evaluation: know that placing the simpler condition first in a compound expression is a performance optimisation, and understand why it works.
True or False: == is used to assign a value to a variable.
Fill in the blank: In a flowchart, a decision structure is represented by a ________ shape.
True or False: In an AND expression, both operands must be true for the result to be true.
Fill in the blank: Short-circuit evaluation means the computer stops evaluating a compound expression as soon as the ________ is determined.
True or False: A case/switch structure can test ranges like "greater than 50."
Answers: 1. False (== compares; = assigns). 2. Diamond. 3. True. 4. Overall result. 5. False (case/switch tests discrete values, not ranges).
Q: What is the difference between = and == in most programming languages?
A: = is the assignment operator (it stores a value in a variable). == is the comparison operator (it checks whether two values are equal and returns true or false).
Q: Given the pseudocode below, what displays when temp is 75?
If temp < 40 Then
Display "Cold"
Else
If temp < 90 Then
Display "Nice weather"
Else
Display "Hot"
End If
End If
A: "Nice weather" displays. 75 is not less than 40, so the outer Else runs. Inside that, 75 is less than 90, so "Nice weather" is chosen.
Q: Evaluate the following expression when length = 10 and width = 5: length > width AND width != 0
A: True. 10 > 5 is true, and 5 != 0 is true. Both sides of the AND are true, so the result is true.
Q: What is the output of NOT(3 > 7)?
A: True. 3 > 7 is false, and NOT flips false to true.
Q: When should you use a case/switch structure instead of an If-Else If chain?
A: When you are comparing a single variable against several specific discrete values (such as integers or characters). Case/switch cannot handle ranges or complex Boolean expressions.
Q: What does short-circuit evaluation mean, and how can you use it to your advantage?
A: Short-circuit evaluation means the computer skips the second operand of an AND or OR expression when the first operand already determines the result. You can exploit this by placing the simpler or more likely-to-short-circuit condition first, which saves processing time.
This module connects directly to repetition structures (loops) covered in the next module, because loops also rely on Boolean conditions to decide when to stop. The logical operators introduced here (AND, OR, NOT) reappear throughout the course whenever you need to write complex conditions, including in loop guards and method logic. Understanding decision structures is also prerequisite to error handling, where your code must decide what to do when something goes wrong.
control structures, decision structures, selection structures, if statement, if-else, if-then-else, else if, elif, nested if, Boolean expression, Boolean logic, Boolean variable, comparison operators, relational operators, equality operator, assignment operator, logical operators, AND, OR, NOT, &&, ||, !, truth table, short-circuit evaluation, short-circuit programming, case structure, switch statement, select case, flowchart diamond, branching, conditional execution, Java if statement, pseudocode if-then, COP 2510, Computer Science I, UCF