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.
DECLARE userInput AS STRING
PRINT "Enter a string input"
INPUT userInput
userInput = input("Enter a string input")
//Scanner is a java.util library
Scanner scn = new Scanner(System.in);
System.out.print("Enter a string input");
String userInput = scn.next();
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.
//Example 1
IF userInput = "am" THEN PRINT "Good Morning" ENDIF
//Example 2
IF userInput = "am" THEN
PRINT "Good Morning"
ELSE
PRINT "Hello"
ENDIF
//Example 3
IF userInput = "am" THEN
PRINT "Good Morning"
ELSE IF userInput = "pm" THEN
PRINT "Good Evening"
ELSE
PRINT "Hello"
ENDIF
#Example 1
if userInput == "am":
print("Good morning")
#Example 2
if userInput == "am":
print("Good morning")
else:
print("Hello")
#Example 3
if userInput == "am":
print("Good morning")
elif userInput == "pm":
print("Hello")
//Example 1
if (userInput == "am") System.out.println("Good morning")
//Example 2
if (userInput == "am")
System.out.println("Good morning");
else
System.out.println("Hello");
//Example 3
if (userInput == "am"){
System.out.println("Good morning");
}
else if(userInput == "pm"){
System.out.println("Good morning");
}
else {
System.out.println("Hello");
}
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.
CASE OF userInput
"am":
PRINT "Good Morning"
"pm":
PRINT "Good Evening"
OTHERWISE:
PRINT "Hello"
ENDCASE
#this only works in Python version 3.10 and above.
#Previous versions do not have this function
match userInput:
case "am":
print("Good morning")
case "pm":
print("Good evening")
case _:
print("Hello")
switch(userInput){
case "am":
System.out.println("Good morning");
break;
case "pm":
System.out.println("Good evening");
break;
default:
System.out.println("Hello");
}
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.
//Example 1
FOR i = 1 TO 10
PRINT "Number " + i
NEXT i//Example 2
SET count <- 1
WHILE count <= 10
PRINT "Number " + count
count = count + 1
ENDWHILE//Example 3
DECLARE num AS Integer
DO
PRINT "Enter a number. 0 to end."
INPUT num
WHILE num > 0//Example 4
DECLARE num AS Integer
REPEAT
PRINT "Enter a number. 0 to end."
INPUT num
UNTIL num = 0
#Example 1
for i in range(1, 11):
print("Number "+ int(i))
#Example 2
while count <= 10:
print("Number "+int(count))
count += 1
//Example 1
for(int i=1; i <= 10; i++){
System.out.println("Number"+ i);
}
//Example 2
int count = 1;
while (count <= 10) {
System.out.println("Number"+ count);
count++;
}
//Example 3
Scanner scn = new Scanner(System.in);
do {
System.out.print("Enter a number, 0 to stop");
int userInt = scn.nextInt();
} while (userInt > 0);
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
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.
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.
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.
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.
temp mail
February 3, 2024My 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.