Basics of Pseudocode

What is Pseudocode?

Pseudocode refers to any code that is not specific to any programming language. This is useful when you have not decided on the programming language to use when you start coding. It is also good for beginners as it helps you understand basic algorithms without going into the details of a specific programming language.

In this post, we will cover the basics of Pseudocode – Input & Output, Conditionals (if else, switch) and Loops. We will also cover commonly asked questions about Pseudocode.

Input & Output

To allow the user to interact with the program, the program must allow  the user to input values. In Pseudocode, the following commands can be used to represent input:
INPUT
READ
*Input is the more commonly used option in IGCSE and O Levels. 

Here are some examples of Pseudocode Input along with Python and Java examples.

You can see that Python only requires 1 line of code while Pseudocode and Java require more lines. In Python, there is no need to declare the data type. In Pseudocode, you are required to DECLARE or SET the variable name (userInput) along with the datatype (STRING).

Conditionals

Next, we will cover conditional statements – If, If-Else, and Select Case. In Pseudocode, all IF statements must end with an ENDIF statement to indicate the end of the IF block. 

Here are some code examples of if statements using Pseudocode, Python and Java.

In Pseudocode, all 3 examples start with IF, and must end with ENDIF. In Example 1, the code is written in a single line. This is valid. However, it is neater to write the code in multiple lines with indentations as shown in Example 3.

In Python, indentation and newline rules must be followed . The indented code under each if statement shows that it will be executed if the condition is True. Another difference with Pseudocode is that Pseudocode uses only 1 “=” in the if statement, while Python and Java requires “==” for conditionals.

In Java, indentation and newlines are not compulsory, but each statement must end with a semicolon (;). Braces ({ }) are required if there are multiple lines of code in the if block but optional if there is only a single line.

Next are code examples of Select Case statement and how it differs in Pseudocode, Python and Java. In Pseudocode, CASE OF, OTHERWISE and ENDCASE are important keywords.

Do take note of the different keywords used in Pseudocode, Python and Java, in particular the catch all case (OTHERWISE, _, default).

Loops

In Pseudocode, there are 4 basic loop types:
FOR …. NEXT
WHILE … ENDWHILE
DO … WHILE
REPEAT … UNTIL

Some loop types are not supported in Python and Java, as you will see from the examples below.

The FOR… NEXT loop is known as a fixed-count loop.  In Python and Java, the for loops are written in a similar way.

Example 2 in Pseudocode is a WHILE loop, which is also known as a pre-condition loop.  Both Python and Java support while loops. 

Example 3 and 4 in Pseudocode are post-condition loops. Python does not have a post-condition loop, while Java has do=while loops, but no repeat-until.

Frequently Asked Questions

Do I need to use capital letters for keywords?

It is not compulsory but recommended. In the exams, you will not be penalized for using input instead of INPUT.  It is recommended to use all capital letters or capitalize the first letter to make your code easier to read. 

Do I need to declare all my variables at the start of the code?

Not necessarily, but you are required to declare variables with the data type before you use them. For example, you want the user to enter a number. 

DECLARE num AS integer
INPUT num

It makes your code more organized if you put "DECLARE num AS integer"  at the start of the code, but writing it before "INPUT num" works as well.

If you do not declare count variables used in while loops and for loops, examiners may be lenient and may not penalize you, However, do try to declare all variables in your code as good practice.

Can I use actual code in Pseudocode?

It depends. In O Levels and A Levels, there may be questions that require you to use string functions like SUBSTRING or UPPERCASE. 

If you forget the pseudocode version, still complete the question by writing the Python or Java functions that you know. Examiners will most likely give you the mark if your code is logically correct.

Do I need to indent Pseudocode?

It is not required but recommended, as it makes your code more readable and organized. 

Messily written code may be penalized by examiners, so do try to practice good code writing with indentation and spacing.

One comment

  • My brother strongly recommended that I visit this website, and he was entirely correct. This post truly brightened my day. You have no idea how much time I had wasted searching for this information. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *