Pseudocode Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Pseudocode.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

An informal, human-readable description of an algorithm using structured language that resembles code but is not tied to any specific programming language. Pseudocode uses plain English mixed with programming constructs like IF, WHILE, and FOR to describe logic without worrying about syntax rules.

Pseudocode is a rough draft for code β€” write the logic in plain English first, then translate to real code.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Pseudocode lets you focus on logic without worrying about syntax. It's a planning tool, not a running program.

Common stuck point: There's no single 'correct' pseudocode format. It just needs to be clear and unambiguous to the reader.

Sense of Study hint: When writing pseudocode, use indentation to show structure (loops, conditionals), use uppercase for keywords (IF, WHILE, FOR, PRINT), and write one action per line. Focus on the logic, not the syntaxβ€”if a human can read it and understand the steps, it is good pseudocode.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

easy
Write pseudocode for a program that asks the user for two numbers and outputs the larger one.

Answer

INPUT a, b. IF a > b THEN OUTPUT a ELSE OUTPUT b. Pseudocode describes logic without language-specific syntax.

First step

1
Step 1: Get the inputs: a = INPUT('Enter first number'). b = INPUT('Enter second number').

Full solution

  1. 2
    Step 2: Compare: IF a > b THEN OUTPUT a. ELSE OUTPUT b.
  2. 3
    Step 3: Pseudocode does not follow the syntax of any specific language β€” it uses plain English-like statements to describe the logic clearly.
Pseudocode is an informal way of describing an algorithm using structured English. It focuses on logic rather than syntax, making it accessible to anyone regardless of programming language knowledge.

Example 2

medium
Convert the following Python code to pseudocode: total = 0; for i in range(1, 11): if i % 2 == 0: total += i; print(total).

Example 3

easy
Write pseudocode to read three numbers and output their sum.

Example 4

medium
Write pseudocode for: read a number n, output 'positive' if n>0, 'negative' if n<0, else 'zero'.

Example 5

medium
Write pseudocode that asks for a password until the user enters 'secret'.

Example 6

hard
Trace a swap: SET a TO 7; SET b TO 2; SET t TO a; SET a TO b; SET b TO t. What are a and b?

Example 7

hard
Trace: SET arr TO [3,1,4,1,5,9,2,6]; bubble-sort one pass swapping adjacent if out of order. Show arr after one pass.

Example 8

challenge
Trace Euclid's algorithm: SET a TO 48; SET b TO 18; WHILE b != 0: SET t TO b; SET b TO a MOD b; SET a TO t. What is a at the end?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
Write pseudocode for a program that reads a list of 10 numbers and outputs how many are above average.

Example 2

hard
A colleague writes pseudocode that mixes Python syntax, uses unclear variable names, and skips steps. Identify three problems with: 'x = input(); for y in x: if y == z: cnt+=1; print(cnt)'. Rewrite it properly.

Example 3

easy
Trace this pseudocode: SET x TO 5; SET y TO 3; SET z TO x + y; OUTPUT z. What is printed?

Example 4

easy
In pseudocode, which keyword introduces a two-way decision?

Example 5

easy
Trace: SET total TO 0; FOR i FROM 1 TO 3: SET total TO total + i. What is total at the end?

Example 6

easy
Which is the BEST pseudocode line, given the rule 'avoid language-specific syntax'? (a) int x = 5; (b) SET x TO 5

Example 7

easy
Trace: SET a TO 4; IF a > 2 THEN OUTPUT "big" ELSE OUTPUT "small". What is printed?

Example 8

easy
What is the main PURPOSE of writing pseudocode before real code?

Example 9

easy
Trace: SET n TO 10; SET n TO n - 4; SET n TO n * 2; OUTPUT n. What is printed?

Example 10

easy
Which control structure repeats steps WHILE a condition stays true?

Example 11

medium
Trace: SET s TO 0; FOR i FROM 1 TO 4: IF i is even THEN SET s TO s + i. What is s?

Example 12

medium
Trace: SET x TO 2; WHILE x < 20: SET x TO x * 3. What is x when the loop ends?

Example 13

medium
Translate to pseudocode intent: a program reads two numbers and prints the larger. Which condition is correct?

Example 14

medium
Trace: SET count TO 0; FOR each c IN "abca": IF c = "a" THEN SET count TO count + 1. What is count?

Example 15

medium
Trace nested logic: SET r TO 0; FOR i FROM 1 TO 2: FOR j FROM 1 TO 2: SET r TO r + 1. What is r?

Example 16

medium
Trace: SET x TO 7; IF x > 10 THEN OUTPUT "A" ELSE IF x > 5 THEN OUTPUT "B" ELSE OUTPUT "C". Printed?

Example 17

medium
A spec says: 'repeat asking for a password until it is correct'. Which loop fits best?

Example 18

medium
Trace: SET p TO 1; FOR i FROM 1 TO 4: SET p TO p * i. What is p (this computes a factorial)?

Example 19

medium
Trace: SET x TO 16; SET steps TO 0; WHILE x > 1: SET x TO x div 2; SET steps TO steps + 1. What is steps?

Example 20

challenge
Trace this swap: SET a TO 3; SET b TO 8; SET t TO a; SET a TO b; SET b TO t. What are a and b at the end?

Example 21

challenge
Trace: SET n TO 13; SET out TO ""; WHILE n > 0: SET out TO (n mod 2) JOINED-BEFORE out; SET n TO n div 2. What is out?

Example 22

challenge
Two pseudocode versions sum 1..n. Version X uses a FOR loop adding each i. Version Y outputs n*(n+1)/2. For n=100, do they agree, and on what value?

Example 23

easy
Trace: SET a TO 3; SET b TO 4; SET c TO a * b; OUTPUT c. What is printed?

Example 24

easy
Trace: SET s TO 0; FOR i FROM 1 TO 5: SET s TO s + i. What is s?

Example 25

easy
Which keyword introduces a loop that repeats a fixed number of times in pseudocode: WHILE or FOR?

Example 26

easy
Trace: SET x TO 10; IF x >= 10 THEN OUTPUT 'yes' ELSE OUTPUT 'no'. What is printed?

Example 27

easy
Trace: SET n TO 5; SET n TO n + 3; SET n TO n * 2. What is n at the end?

Example 28

medium
Trace: SET total TO 0; FOR i FROM 1 TO 6: IF i MOD 2 = 1 THEN SET total TO total + i. What is total?

Example 29

medium
Trace: SET x TO 1; WHILE x < 50: SET x TO x * 2. What is x when the loop ends?

Example 30

medium
Trace nested loops: SET r TO 0; FOR i FROM 1 TO 3: FOR j FROM 1 TO 3: SET r TO r + 1. What is r?

Example 31

medium
Trace: SET p TO 1; FOR i FROM 1 TO 5: SET p TO p * i. What is p (a factorial)?

Example 32

medium
Trace: SET count TO 0; FOR each c IN "hello": IF c = 'l' THEN SET count TO count + 1. What is count?

Example 33

medium
Trace: SET x TO 20; SET steps TO 0; WHILE x > 1: SET x TO x div 2; SET steps TO steps + 1. What is steps?

Example 34

medium
Trace: SET arr TO [4, 7, 2, 9, 5]; SET m TO arr[0]; FOR i FROM 1 TO 4: IF arr[i] > m THEN SET m TO arr[i]. What is m?

Example 35

medium
What is wrong with this pseudocode for the rule 'avoid language-specific syntax': `for(int i=0;i<10;i++){total+=i;}`

Example 36

hard
Trace: SET n TO 10; SET out TO ''; WHILE n > 0: SET out TO (n MOD 2) PREPENDED-TO out; SET n TO n DIV 2. What is out?

Example 37

hard
Write pseudocode that outputs how many numbers in a list of 10 are above the average.

Example 38

hard
Trace linear search: SET arr TO [3, 8, 5, 9, 2]; SET target TO 9; FOR i FROM 0 TO 4: IF arr[i] = target THEN OUTPUT i AND STOP. What is output?

Example 39

hard
Trace: SET x TO 81; SET steps TO 0; WHILE x > 1: SET x TO x DIV 3; SET steps TO steps + 1. What is steps?

Example 40

challenge
Two pseudocode versions sum 1..n. Version X loops adding each i. Version Y outputs n*(n+1)/2. For n=200, do they agree, and on what value?

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithm