Pseudocode is a way to represent algorithms or program logic using a mixture of natural language and simple programming constructs. It helps in planning and designing the structure of a program without worrying about the specific syntax of a programming language.
Most programs are developed using programming languages. These languages have specific syntax that must be used so that the program will run properly. Pseudocode is not a programming languageπ, it is a simple way of describing a set of instructions that does not have to use specific syntax.
Pseudo Code Notation π§©
There is no strict set of standard notations for pseudocode, but some of the most widely recognized are:
Syntax | Description |
---|---|
SET | allows you to specify the name of the variable and assign a value to it |
INPUT | indicates a user will be inputting something |
OUTPUT | indicates that an output will appear on the screen |
WHILE | a loop (iteration that has a condition at the beginning) |
FOR | a counting loop (iteration) |
REPEAT - UNTIL | a loop (iteration) that has a condition at the end |
IF - THEN - ELSE | a decision (selection) in which a choice is made |
Write Pseudo Codes π
In pseudocode, it is customary to include a BEGIN
keyword at the beginning to signify the start of the program, just as in a flowchart we use a "Start" terminal. Additionally, it is essential to include an END
keyword at the end to indicate the conclusion of the program, similar to the "Stop" terminal in a flowchart.
-
Declaring your variables:
- SET : To indicate that the user will be setting a value or variable in pseudocode, you can use the
SET
keyword.
SET name = "John" SET age = 25
In pseudocode, when assigning a value to a variable, we typically use double quotation marks (
""
or''
) to indicate that the value is a word or a string. On the other hand, when the value is an integer or a numeric value, we simply use the number itself without any quotation marks. - SET : To indicate that the user will be setting a value or variable in pseudocode, you can use the
-
Using Input and Output in Pseudo Code:
-
INPUT : To indicate that the user will be inputting something in pseudocode, you can use the
INPUT
keyword.INPUT "Enter your name: " as name
This example prompts the user to enter their name and stores the input in the variable
name
. -
OUTPUT : To indicate that an output will appear on the screen in pseudocode, you can use the
OUTPUT
keyword.OUTPUT "Hello, Welcome to the program."
This example outputs a greeting message.
-
-
Use standard programming constructs: Pseudocode uses common programming constructs like loops, conditionals:
-
Selection: Checks a condition and performs an action based on the result.
IF β THEN β ELSE : A decision in pseudocode, where a choice is made based on a condition, can be represented using the IF - THEN - ELSE structure.
BEGIN IF age >= 18 THEN OUTPUT "You are eligible to vote." ELSE OUTPUT "Sorry, you are not eligible to vote yet." END IF END
This example checks if the variable age is greater than or equal to 18. If the condition is true, it outputs a message indicating eligibility to vote. Otherwise, it outputs a message stating that the person is not eligible to vote yet.
Using the
IF - THEN - ELSE
structure allows you to create decision-making processes in pseudocode. It enables the program to choose between different paths of execution based on the evaluation of a condition.The keyword
THEN
is optional in some pseudocode conventions. So, it can be written asIF age >= 18
without explicitly usingTHEN
.By using
IF END
, you ensure that all the statements within the conditional block are enclosed, making it clear which statements are executed based on the condition. -
Looping: Executes a set of instructions repeatedly until a certain condition is met:
WHILE β DO : A loop in pseudocode that has a condition at the beginning is commonly represented by the
WHILE
keyword.BEGIN SET count = 0 WHILE count < 5 DO OUTPUT "Count: " + count SET count = count + 1 END WHILE END
This example demonstrates a
WHILE
loop that continues iterating as long as the count variable is less than 5. It outputs the value of count on each iteration.Using the
WHILE
keyword helps to signify a loop that iterates based on a condition that is checked at the beginning of each iteration. It ensures that the loop will continue executing as long as the condition remains true.By using
WHILE END
, you can easily identify the beginning and end of the loop, ensuring that the loop's body is clearly defined and contained within the loop structure.FOR β TO β DO : A counting loop in pseudocode is commonly represented by the
FOR
keyword. It allows for a specific number of iterations based on a defined range or condition.BEGIN FOR i = 1 TO 5 DO OUTPUT "Iteration: " + i END FOR END
This example uses a
FOR
loop to iterate from 1 to 5. It outputs a message indicating the current iteration number.By using the
FOR
-TO
-DO
structure, you establish a counting loop where the counter variable is automatically incremented or decremented with each iteration until a specific range or condition is met. TheTO
keyword represents the range of values that the loop will iterate over.By using
FOR END
, you establish a clear boundary for the iteration block, making it evident which statements are executed within the loop and helping to prevent errors in cases where additional code should not be part of the loop.REPEAT β UNTIL : A loop in pseudocode that has a condition at the end can be represented using the
REPEAT
UNTIL
structure.BEGIN SET number = 0 REPEAT INPUT "Enter a positive number: " as number UNTIL number > 0 END
This example repeatedly prompts the user to enter a number until they input a positive number. The loop continues until the condition
number > 0
is met.Using the
REPEAT
-UNTIL
structure helps create a loop where the condition is evaluated at the end of each iteration. This ensures that the loop will execute at least once, as the condition is checked after the initial iteration.
-
General Rules βοΈ
π Always capitalize the initial word (often one of the main six constructs).
π Make only one statement per line.
π Indent to show hierarchy, improve readability, and show nested constructs.
π Always end multi-line sections using any of the END
keywords (ENDIF
, ENDWHILE
, etc.).
π Keep your statements programming language independent.
π Keep it simple, concise and readable.