๐ฎ Pseudocode
Pseudocode is a plain-language way to describe algorithms and logic without worrying about programming syntax.
Itโs ideal for planning, teaching, and auditing workflows.
๐ ๏ธ Features
| Principle | Description |
|---|---|
| Language-agnostic | Avoid specific programming syntax; use plain English |
| Structured | Use control flow keywords (IF, FOR, WHILE, etc.) |
| Readable | Indent blocks and use whitespace for clarity |
| Declarative | Focus on what the algorithm does, not how itโs implemented |
| Consistent | Maintain uniform naming and formatting throughout |
โ Best Practices
STARTandENDto denote blocks of code- The start and end of the whole code
- The start and end of functions, loops and conditionals (e.g. FUNCTION … END FUNCTION, FOR … END FOR, IF … END IF)
DECLAREfor introducing new variables (e.g., DECLARE x)SETfor assigning values to variables (e.g., SET x to 5)NOT,AND,ORfor readable logic operatorsMOD,DIVfor readable arithmetic operatorsIF,ELSE,ELSE IFfor conditionslsWHILE,FOR,IN,OFfor looping (e.g. FOR each item IN list, FOR key IN dictionary)REPEAT ... UNTIL conditioncan alternatively be usedEXIT/BREAKto leave the loop early,CONTINUEto skip to the next iterationINPUT/OUTPUTorPRINTto define user input and function output//for comments, to explain non-obvious stepsTRY,CATCH,RAISE ERRORfor error-handlingFUNCTION (params)andRETURNto define reusable logicCALLto invoke a function- Avoid data types, semicolons, or language-specific syntax
- Use indentation and spacing for readability
- Use section headers (
// --- Section Name ---) for long logic blocks
๐ธ Example
// --- Function Definition ---
FUNCTION computeAverage(sum, quantity)
TRY
IF quantity == 0 THEN
RAISE ERROR "Division by zero"
ELSE
SET average to sum DIV quantity
PRINT "Average of even numbers: " + average
END IF
CATCH error
PRINT "Error: " + error
END TRY
END FUNCTION
START
// --- Input ---
INPUT numberList
// --- Processing ---
DECLARE total
DECLARE count
SET total to 0
SET count to 0
FOR each number IN numberList
IF number MOD 2 == 0 THEN
SET total to total + number
SET count to count + 1
END IF
END FOR
// --- Output ---
IF count == 0 THEN
PRINT "No even numbers found."
ELSE
CALL computeAverage(total, count)
END IF
ENDLast updated on