Conventions used in this module
| Purpose | Keyword and form |
|---|---|
| Start and end | BEGIN ... END |
| Input | READ name |
| Output | PRINT value |
| Assignment | total = total + i |
| Selection | IF condition THEN ... ELSE ... END IF |
| Pre-test loop | WHILE condition DO ... END WHILE |
| Counted loop | FOR i = 1 TO n DO ... END FOR |
| Post-test loop | REPEAT ... UNTIL condition |
| Procedure | PROCEDURE name(parameters) ... END PROCEDURE |
Indent the body of every structure. Use meaningful variable names. Some books write assignment as total ← total + i; this module uses =.
Operators you can use
| Kind | Operators | Example |
|---|---|---|
| Arithmetic | + - * / | area = length * width |
| Whole-number division and remainder | DIV, MOD | 17 DIV 5 is 3, 17 MOD 5 is 2 |
| Comparison | = != < <= > >= | IF marks >= 50 THEN |
| Logical | AND OR NOT | IF age >= 18 AND hasID THEN |
MOD (the remainder) is very useful: n MOD 2 = 0 tests whether n is even, and n MOD d = 0 tests whether d divides n exactly.
The three loops compared
| Loop | When the condition is tested | Smallest number of passes | Use it when |
|---|---|---|---|
FOR i = 1 TO n | Before each pass (it counts) | 0 | You know how many times to repeat |
WHILE condition DO | Before each pass | 0 | You repeat while something is true and might not need to run at all |
REPEAT ... UNTIL condition | After each pass | 1 | The body must run at least once, for example asking for a password |
Note the different meaning of the condition: WHILE repeats while the condition is true; REPEAT ... UNTIL repeats until the condition becomes true.
Rules for good pseudocode
- One statement per line, with keywords in capitals.
- Indent the body of every IF, loop and procedure, and close each one (END IF, END WHILE, END FOR).
- Use meaningful names:
totalMarkstells the reader much more thant. - Write what happens, not language details:
READ marks, notScanner sc = new Scanner(System.in). - Be precise enough that another programmer could turn it into code without asking you anything.
Flowchart or pseudocode?
Flowcharts show the overall flow visually and suit beginners and presentations. Pseudocode is faster to write, easier to edit and closer to real code. Professionals usually use pseudocode.
From pseudocode to Java
Because pseudocode is close to real code, each line translates almost directly:
| Pseudocode | Java |
|---|---|
READ marks | int marks = input.nextInt(); (with a Scanner called input) |
PRINT total | System.out.println(total); |
IF a > b THEN ... ELSE ... END IF | if (a > b) { ... } else { ... } |
WHILE x != 0 DO ... END WHILE | while (x != 0) { ... } |
FOR i = 1 TO n DO ... END FOR | for (int i = 1; i <= n; i++) { ... } |
REPEAT ... UNTIL done | do { ... } while (!done); |
n MOD 2 | n % 2 |
a = b (comparison in a condition) | a == b |
Watch the last row: in pseudocode = is used both to assign and to compare, but Java uses = to assign and == to compare. Also note that REPEAT ... UNTIL becomes a do ... while loop with the condition reversed.
Remember
- Pseudocode is for people, so clarity matters more than strict rules.
- Indent and close every structure.
- WHILE may run zero times; REPEAT ... UNTIL always runs at least once.