Sub topic 2
Conventions That Keep Pseudocode Unambiguous
This chapter assumes you can write basic programs with variables, conditionals, and loops in at least one language. The aim is to fix a small set of conventions so that every line of pseudocode has exactly one meaning. A trace you do by hand is only reliable if the text you are tracing cannot be read two ways.
Assignment Versus Comparison
Many languages use = both to store a value and, in some contexts, to test equality. Pseudocode avoids this by giving each its own symbol.
x ← 5means "store 5 in x". Read it as "x gets 5".x = 5means "is x equal to 5?" and produces true or false.x ≠ 5,x ≤ 5, andx ≥ 5are the other comparisons.
count ← 0
count ← count + 1
IF count = 1 THEN
PRINT "first item"
END IFThe line count ← count + 1 reads the old value of count, adds 1, and stores the result. In a trace table, that line changes the count column from 0 to 1.
Keywords and Block Endings
Write keywords in capitals so they stand apart from variable names. Close every block with a matching end keyword. This makes the scope of each block visible even if indentation is lost in a document or email.
| Construct | Opening | Closing |
|---|---|---|
| Selection | `IF condition THEN` / `ELSE` | `END IF` |
| Counted loop | `FOR i ← 1 TO n DO` | `END FOR` |
| Conditional loop | `WHILE condition DO` | `END WHILE` |
| Function | `FUNCTION Name(params)` | `END FUNCTION` |
| Procedure | `PROCEDURE Name(params)` | `END PROCEDURE` |
| Output of a value | `RETURN value` | none |
Later chapters cover how each structure behaves. Here the point is that each one has a fixed spelling.
Indentation
Indent the body of every block by the same amount, usually four spaces. Indentation and end keywords should agree. If they disagree, the reader has to guess which one you meant, and that guess is a common source of tracing errors.
FOR i ← 1 TO 3 DO
IF i = 2 THEN
PRINT "two"
END IF
PRINT i
END FORHere PRINT i runs on every iteration because it sits inside FOR but outside IF. If you moved it one level deeper, it would run only when i = 2.
Other Choices to State Up Front
Some conventions differ between textbooks. State the ones you use at the top of the document, or follow your course's standard.
- Array indexing: say whether arrays start at index 1 or index 0. This tutorial uses 1-based indexing, and a later chapter covers the difference.
- Range ends:
FOR i ← 1 TO nincludesn. - Boolean operators: write
AND,OR,NOTin capitals, and say whetherANDshort-circuits, meaning it skips the second test when the first one is false. - Comments: use
//for notes that are not executed.
Exercise
Rewrite this fragment using the conventions in this chapter. The intent is: if total equals 10, reset it to zero, and always print total afterward. if total = 10 total = 0 print total
Show answer
IF total = 10 THEN / (indented) total ← 0 / END IF / PRINT total. The first = stays because it compares values. The second becomes ← because it stores a value. END IF closes the block, and PRINT total sits outside the block at the outer indentation level, so it always runs.
Exercise
In the indentation example above, how many lines of output are printed, and what are they?
Show answer
Four lines: 1, "two", 2, 3. "two" is printed only when i = 2, and it comes before that iteration's PRINT i.