This chapter sets a single, consistent pseudocode style guide that we will use everywhere in the course. The goal is not to mimic any one programming language, but to make algorithms easy to read, easy to translate into code, and hard to misinterpret.
1) Standard Keywords (Course Style Guide)
Use uppercase for keywords and keep variable names in lower_snake_case (e.g., total_score, max_value). Indent blocks consistently (2 or 4 spaces) and always close a block with an explicit end marker.
INPUT
Purpose: Declare what information the algorithm receives.
- Write inputs as a short list.
- Use descriptive names and include units when helpful.
INPUT: numbers (a list of integers), target (integer)OUTPUT
Purpose: Declare what the algorithm produces.
OUTPUT: found_index (integer or -1)SET
Purpose: Assign a value to a variable. Use SET for assignment and reserve = for comparisons (see operators section).
Continue in our app.
You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.
Or continue reading below...Download the app
SET count = 0
SET total = 0
SET is_valid = TRUEIF / THEN / ELSE
Purpose: Branch based on a condition. Put the condition on the same line as IF. Use THEN and end with END IF.
IF age >= 18 THEN
OUTPUT "adult"
ELSE
OUTPUT "minor"
END IFFOR
Purpose: Counted loops or looping through a known range. Prefer inclusive ranges written clearly.
FOR i FROM 1 TO n DO
OUTPUT i
END FORIf iterating over a list, use an index when you need positions:
FOR i FROM 0 TO length(items) - 1 DO
OUTPUT items[i]
END FORWHILE
Purpose: Loop while a condition remains true. Ensure the loop changes something that can make the condition false.
WHILE attempts < 3 AND is_locked = TRUE DO
SET attempts = attempts + 1
END WHILEREPEAT / UNTIL
Purpose: Loop that runs at least once. The condition is checked at the end.
REPEAT
INPUT: guess
UNTIL guess = secretFUNCTION / PROCEDURE
Purpose: Package reusable logic. Use FUNCTION when you return a value; use PROCEDURE when you perform actions (often with outputs via parameters or side effects).
FUNCTION add(a, b)
RETURN a + b
END FUNCTIONPROCEDURE print_receipt(items)
FOR i FROM 0 TO length(items) - 1 DO
OUTPUT items[i]
END FOR
END PROCEDURERETURN
Purpose: End a function and provide a value. Use a single return value unless you explicitly define multiple outputs.
FUNCTION is_even(n)
IF n % 2 = 0 THEN
RETURN TRUE
ELSE
RETURN FALSE
END IF
END FUNCTION2) Common Operators and Comparisons
To avoid confusion, this course uses SET for assignment and uses operators only inside expressions and conditions.
Arithmetic and update patterns
+addition,-subtraction,*multiplication,/division%remainder (modulo), useful for even/odd checks
SET total = total + price
SET i = i + 1
SET remainder = n % 2Comparisons
=equal to!=not equal to<less than<=less than or equal to>greater than>=greater than or equal to
IF score != 0 THEN
OUTPUT "non-zero"
END IFLogical operators
AND: both conditions must be trueOR: at least one condition must be trueNOT: flips true/false
IF (age >= 18) AND (has_id = TRUE) THEN
OUTPUT "allow"
END IFTip: Use parentheses when combining multiple logical parts, even if you think precedence is obvious. It improves readability and reduces mistakes.
3) Writing Conditions in Readable English-Like Form
A readable condition usually has three parts: left value, comparison, right value. When combining conditions, group them into meaningful chunks.
Guidelines
- Prefer positive conditions when possible:
is_valid = TRUEis often clearer thanNOT is_invalid. - Name booleans like statements:
is_empty,has_discount,is_logged_in. - Avoid “magic numbers” in conditions; assign them to named constants when they matter.
- Keep conditions short; if a condition becomes long, break it into named helper variables.
Step-by-step: turning a messy condition into readable pseudocode
Messy:
IF NOT(a<18 OR id=0) THEN ...Step 1: Add spacing and parentheses to show intent
IF NOT ( (a < 18) OR (id = 0) ) THEN
...
END IFStep 2: Replace unclear names with meaningful ones
IF NOT ( (age < 18) OR (id = 0) ) THEN
...
END IFStep 3: Prefer a positive statement by rewriting the logic
IF (age >= 18) AND (id != 0) THEN
...
END IFReadable multi-part conditions with helper variables
If a condition has multiple business rules, name them:
SET is_adult = (age >= 18)
SET has_valid_id = (id != 0)
SET can_enter = is_adult AND has_valid_id
IF can_enter THEN
OUTPUT "allow"
ELSE
OUTPUT "deny"
END IF4) Reusable Patterns (Counters, Accumulators, Min/Max, Searching)
Patterns are small algorithm “building blocks” you will reuse constantly. Each pattern below includes a template and a short example.
Pattern A: Counter
Use when: you need to count how many times something happens.
Template:
SET count = 0
FOR each item in collection DO
IF condition(item) THEN
SET count = count + 1
END IF
END FOR
OUTPUT countExample: count how many numbers are negative
INPUT: numbers
OUTPUT: negative_count
SET negative_count = 0
FOR i FROM 0 TO length(numbers) - 1 DO
IF numbers[i] < 0 THEN
SET negative_count = negative_count + 1
END IF
END FOR
OUTPUT negative_countPattern B: Accumulator (Sum / Product / Combined result)
Use when: you need a running total (sum, product, concatenation).
Template (sum):
SET total = 0
FOR each item in collection DO
SET total = total + value(item)
END FOR
OUTPUT totalExample: sum of prices
INPUT: prices
OUTPUT: total
SET total = 0
FOR i FROM 0 TO length(prices) - 1 DO
SET total = total + prices[i]
END FOR
OUTPUT totalPattern C: Maximum / Minimum Tracking
Use when: you need the largest/smallest value seen so far.
Step-by-step approach:
- Initialize
max_value(ormin_value) to the first element. - Loop through the remaining elements.
- Update when you find a better candidate.
Example: find maximum number
INPUT: numbers (non-empty list)
OUTPUT: max_value
SET max_value = numbers[0]
FOR i FROM 1 TO length(numbers) - 1 DO
IF numbers[i] > max_value THEN
SET max_value = numbers[i]
END IF
END FOR
OUTPUT max_valueVariation: track both value and position
INPUT: numbers (non-empty list)
OUTPUT: max_value, max_index
SET max_value = numbers[0]
SET max_index = 0
FOR i FROM 1 TO length(numbers) - 1 DO
IF numbers[i] > max_value THEN
SET max_value = numbers[i]
SET max_index = i
END IF
END FOR
OUTPUT max_value
OUTPUT max_indexPattern D: Searching (Linear Search)
Use when: you need to find whether an item exists in a list (and possibly where).
Template:
SET found_index = -1
FOR i FROM 0 TO length(items) - 1 DO
IF items[i] = target THEN
SET found_index = i
STOP LOOP
END IF
END FOR
OUTPUT found_indexBecause “STOP LOOP” is not one of our core keywords, we express it using a boolean flag (course standard) so the control flow stays explicit.
Course-standard version (with a flag):
INPUT: items, target
OUTPUT: found_index
SET found_index = -1
SET i = 0
SET found = FALSE
WHILE (i < length(items)) AND (found = FALSE) DO
IF items[i] = target THEN
SET found_index = i
SET found = TRUE
ELSE
SET i = i + 1
END IF
END WHILE
OUTPUT found_indexReference Table of Conventions (Course Standard)
| Category | Convention | Example |
|---|---|---|
| Keywords | Uppercase keywords | IF ... THEN, FOR ... DO, END IF |
| Variables | lower_snake_case | total_cost, max_index |
| Assignment | Use SET (not =) | SET total = 0 |
| Equality test | Use = inside conditions | IF x = 10 THEN |
| Not equal | Use != | IF name != "" THEN |
| Logic | Use AND, OR, NOT with parentheses | IF (a > 0) AND (b > 0) THEN |
| Blocks | Indent consistently; close blocks explicitly | END IF, END FOR, END WHILE |
| Ranges | FOR i FROM start TO end DO | FOR i FROM 0 TO n - 1 DO |
| Functions | FUNCTION name(params) + RETURN | FUNCTION area(r) RETURN ... |
| Procedures | PROCEDURE name(params) for actions | PROCEDURE print_list(items) |
| Booleans | Use TRUE/FALSE | SET found = FALSE |
Micro-Exercises: Rewrite into the Course Standard
For each exercise: (1) rewrite using the course keywords and conventions, (2) fix assignment vs comparison, (3) add missing end markers, (4) improve naming if needed.
Exercise 1: Assignment vs comparison
Inconsistent pseudocode:
input x
if x = 5
print("five")Rewrite target: Use INPUT:, IF ... THEN, OUTPUT, and END IF. Ensure = is used only for comparison (no assignment needed here).
Exercise 2: Mixed keyword styles and missing end markers
Inconsistent pseudocode:
Input: n
set total to 0
For i = 1 to n
total = total + i
Output totalRewrite target: Use SET total = 0, FOR i FROM 1 TO n DO, and END FOR. Keep OUTPUT: consistent.
Exercise 3: Unclear condition and NOT usage
Inconsistent pseudocode:
IF NOT(userLoggedIn) OR age<18 THEN
OUTPUT "deny"
ELSE
OUTPUT "allow"Rewrite target: Use boolean naming like is_logged_in. Add parentheses and end marker. Consider rewriting into a positive allow-condition.
Exercise 4: WHILE loop with unclear updates
Inconsistent pseudocode:
while password != correct
tries = tries + 1
if tries > 3 then breakRewrite target: Use WHILE with a combined condition (no break). Use SET for updates. Add END WHILE and END IF.
Exercise 5: Max tracking with wrong initialization
Inconsistent pseudocode:
SET max = 0
FOR each x in numbers
IF x > max THEN max = xRewrite target: Assume numbers can contain negatives. Initialize from the first element and use explicit block structure.
Exercise 6: Searching with inconsistent outputs
Inconsistent pseudocode:
function find(list, t)
for i in list
if i == t return true
return falseRewrite target: Use FUNCTION, RETURN, and course comparisons (=, not ==). Decide whether you return a boolean or an index; then make OUTPUT consistent with that choice.