Syntax Rules for Pseudocode
- Natural Language: Use simple and clear natural language to describe steps.
- Keywords: Use standard control flow keywords such as:
- IF, ELSE, ENDIF
- FOR, WHILE, ENDWHILE
- FUNCTION, CALL
- INPUT, OUTPUT
- Indentation: Indent blocks within loops or conditionals to signify nesting.
- Capitalization: Write pseudocode keywords in UPPERCASE to distinguish them from variables and logic descriptions.
- Variables: Use meaningful and descriptive names (e.g.,
customerName
,totalSales
).
Standards:
- Use simple language: Avoid complex sentences and focus on concise, easy-to-understand statements.
- Be consistent: Use the same terminology and formatting throughout the pseudocode.
- Use indentation: Indent code blocks to show hierarchy and improve readability.
Terms
- INPUT: Used to describe user input or data input.
Example:INPUT name
- OUTPUT: Used to describe output or results.
Example:OUTPUT "Hello, " + name
- SET: Used to assign a value to a variable.
Example:SET count = 0
- IF: Used to describe conditional statements.
Example:IF age > 18 THEN ...
- WHILE: Used to describe loops.
Example:WHILE count < 10 DO ...
- FOR: Used to describe loops with a counter.
Example:FOR i = 1 TO 10 DO ...
- REPEAT: Used to describe loops that repeat until a condition is met.
Example:REPEAT ... UNTIL count = 10
- UNTIL: Used to describe loops that repeat until a condition is met.
Example:REPEAT ... UNTIL count = 10
- CASE: Used to describe multi-branch conditional statements.
Example:CASE color OF ...
- PROCEDURE: Used to describe a subroutine or function.
Example:PROCEDURE greet(name) ...
Common Terms and Constructs in Pseudocode
Control Structures
Conditionals:
IF condition
THEN // Actions
ELSE // Alternative actions
ENDIF
LoopsFOR variable FROM start TO end
DO // Actions
ENDFOR
WHILE condition
DO // Actions
ENDWHILE
Input/Output
Use clear and direct terms for input and output:
INPUT "Enter a number:",
user Number
OUTPUT "The result is:", result
Functions and Procedures
Define reusable components:FUNCTION calculateSum(a, b)
RETURN a + b
ENDFUNCTION
Error Handling
Describe how to handle errors in a readable way:IF error occurs
THEN OUTPUT "Error encountered. Exiting process."
EXIT
ENDIF
Data Manipulation
Data operations like adding, updating, or deleting: SET total TO total + itemCost
REMOVE item FROM shoppingCart
Example of Good Pseudocodes
Here’s an example of a simple algorithm to calculate the average of three numbers:
INPUT num1, num2, num3
SET sum = num1 + num2 + num3
SET average = sum / 3
OUTPUT "The average is: " + average
Problem: Calculate the factorial of a given number.
START
INPUT "Enter a positive integer:", num
IF num < 0 THEN
OUTPUT "Error: Number must be positive."
EXIT
ENDIF
SET factorial TO 1
FOR i FROM 1 TO num DO
SET factorial TO factorial * i
ENDFOR
OUTPUT "The factorial of", num, "is", factorial
END
By following these guidelines, syntax, standards, and terms, you can write perfect pseudocode that is easy to read, understand, and implement.