Free Ebook cover Pseudocode Mastery for Beginners

Pseudocode Mastery for Beginners

New course

10 pages

Pseudocode Conventions: Keywords, Operators, and Common Patterns

Capítulo 2

Estimated reading time: 8 minutes

+ Exercise

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 App

Download the app

SET count = 0
SET total = 0
SET is_valid = TRUE

IF / 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 IF

FOR

Purpose: Counted loops or looping through a known range. Prefer inclusive ranges written clearly.

FOR i FROM 1 TO n DO
    OUTPUT i
END FOR

If iterating over a list, use an index when you need positions:

FOR i FROM 0 TO length(items) - 1 DO
    OUTPUT items[i]
END FOR

WHILE

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 WHILE

REPEAT / UNTIL

Purpose: Loop that runs at least once. The condition is checked at the end.

REPEAT
    INPUT: guess
UNTIL guess = secret

FUNCTION / 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 FUNCTION
PROCEDURE print_receipt(items)
    FOR i FROM 0 TO length(items) - 1 DO
        OUTPUT items[i]
    END FOR
END PROCEDURE

RETURN

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 FUNCTION

2) 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 % 2

Comparisons

  • = 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 IF

Logical operators

  • AND: both conditions must be true
  • OR: at least one condition must be true
  • NOT: flips true/false
IF (age >= 18) AND (has_id = TRUE) THEN
    OUTPUT "allow"
END IF

Tip: 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 = TRUE is often clearer than NOT 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 IF

Step 2: Replace unclear names with meaningful ones

IF NOT ( (age < 18) OR (id = 0) ) THEN
    ...
END IF

Step 3: Prefer a positive statement by rewriting the logic

IF (age >= 18) AND (id != 0) THEN
    ...
END IF

Readable 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 IF

4) 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 count

Example: 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_count

Pattern 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 total

Example: 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 total

Pattern C: Maximum / Minimum Tracking

Use when: you need the largest/smallest value seen so far.

Step-by-step approach:

  • Initialize max_value (or min_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_value

Variation: 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_index

Pattern 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_index

Because “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_index

Reference Table of Conventions (Course Standard)

CategoryConventionExample
KeywordsUppercase keywordsIF ... THEN, FOR ... DO, END IF
Variableslower_snake_casetotal_cost, max_index
AssignmentUse SET (not =)SET total = 0
Equality testUse = inside conditionsIF x = 10 THEN
Not equalUse !=IF name != "" THEN
LogicUse AND, OR, NOT with parenthesesIF (a > 0) AND (b > 0) THEN
BlocksIndent consistently; close blocks explicitlyEND IF, END FOR, END WHILE
RangesFOR i FROM start TO end DOFOR i FROM 0 TO n - 1 DO
FunctionsFUNCTION name(params) + RETURNFUNCTION area(r) RETURN ...
ProceduresPROCEDURE name(params) for actionsPROCEDURE print_list(items)
BooleansUse TRUE/FALSESET 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 total

Rewrite 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 break

Rewrite 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 = x

Rewrite 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 false

Rewrite target: Use FUNCTION, RETURN, and course comparisons (=, not ==). Decide whether you return a boolean or an index; then make OUTPUT consistent with that choice.

Now answer the exercise about the content:

In the course-standard linear search that avoids “STOP LOOP”, what is the main reason for using a boolean flag (e.g., found) in the WHILE condition?

You are right! Congratulations, now go to the next page

You missed! Try again.

The style guide avoids non-core keywords like a break/STOP LOOP. A boolean flag (found) is updated when the target is found, and the WHILE condition uses it to stop looping in an explicit, readable way.

Next chapter

Readable Structure: Indentation, Blocks, and Step Grouping in Pseudocode

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.