Do you think programming is for you? Are you planning to do so but don't know where to start? Start with pseudocode since it is really the kickstart of logic building.
Writing code is only half the battle because the real work happens in planning the logic. If you want to save time, avoid bugs, and organise your thoughts like a seasoned developer, mastering pseudocode is essential. You do not have to learn pseudocode because it is quite literally your own way of writing code. In this complete guide, I’ll break down exactly what pseudocode is and how to use it effectively.
What is pseudocode?
Pseudocode is mind-mapping intended program using words like "If, then, otherwise, not, yes, no"—which is simple, understandable english. It is commonly used as an editing tool after which it can be written in preferred programming language. Pseudocode is basically not worrying about syntax, semicolons but about the possible logical errors. An everyday example, like making a cup of tea, in descriptive pseudocode would be:
IF the kettle is empty:
Fill the kettle with water
Put the kettle on to boil
Place a teabag in the mug
IF the water is boiling:
Pour water into the mug
Wait 3 minutes
Remove the teabag
Add milk and sugar
How to use it?
The number one rule is to break down your program into actual step-by-step functions. Basic organisation is key. Use:
- Indentation (margin or tabbing)
- Capital letters
- Symbols for comments (for example a "//" in most cases)
Then, organise your program map using pseudocode rules. Use:
- Input/Output statements
- Selection or decision statements. The two main statements used are IF...THEN...ELSE..ENDIF and CASE...OF...OTHERWISE...ENDCASE.
- Loops. The three common loops in pseudocode are FOR...TO...NEXT, WHILE...DO...ENDWHILE and REPEAT...UNTIL
- A signal of starting or ending statements. For example, when taking decisions, statements end with "ENDIF" or "ENDCASE" which, with indentation, connect the start and end of each statement.
- Modules (procedures and functions)
Input/Output Statements
These are used to simply make an entry or display a subject. For example, to directly enter a number, a statement similar to the one below is appropriate:
INPUT 5
or to enter a number in a variable, num, (variable and constants are explained later; however, a variable stores an entity that can be handled efficiently throughout the program):
INPUT num
Similarly, a statement to display the variable num would be written as follows:
OUTPUT num
Selection Statements
To manage decision-making in your code, selection statements, like the one in the very first example, are used. These give suitable routes to carrying out tasks without making constant errors. For example, if some variable Switch is ON or OFF, certain actions would follow in pseudocode:
IF Switch = Yes:
Switch = No
ELSE IF Switch = No
Switch = Yes
ENDIF
Here, ENDIF is used to mark the end of the decision. Indentation is also used. However, for decisions with multiple outcomes, CASE...OF...OTHERWISE...ENDCASE is the preferred statement. Here's a realistic example to get your thinking going:
DECLARE Weight : REAL
OUTPUT "Please enter the baggage weight in kg:"
INPUT Weight
CASE OF Weight
0.1 TO 14.9 : OUTPUT "Category: Light"
15.0 TO 23.0 : OUTPUT "Category: Standard"
23.1 TO 32.0 : OUTPUT "Category: Heavy"
OTHERWISE OUTPUT "Error: Invalid weight or requires manual oversized handling."
ENDCASE
Here, the variable Weight has been initialised (DECLARE) using the datatype REAL. Datatypes are explained later. Many programming languages require initialisation, therefore it is a good practise to do so.
Loops
Loops handle the repetition of tasks. In case of iterative program statements, the three types of loops mentioned above are used.
The For loop is called the count-controlled loop and the While as well as the REPEAT...UNTIL are condition-controlled loops. This means that a condition is needed in order to end the iteration. The following example shows how to use the first loop.
FOR student = 1 TO 5
OUTPUT "Printing report card for Student number " & student
ENDFOR student
The variable student is iterated 5 times to print the string "Printing report card for Student number" with the student number concatenated (attached) at the end of string. Instead of "NEXT", ENDFOR is used which has the same use as the former and marks the end of the loop.
The second loop on the list allows us to determine the condition of repetition at the start. Look at the following example:
student = 1
While student <= 5 Do
Print "Printing report card for Student number " & student
student = student + 1
EndWhile
Here, student has been initialised to a value of 1 which tracks the count for our loop. The rest works the same as the For loop.
The REPEAT...UNTIL loop allows us to execute the iteration at least once which is why it is also known as the post-condition loop. Here is how it is used:
student = 1
Repeat
Print "Printing report card for Student number " & student
student = student + 1
Until student > 5
The loop is the same as the While loop; however, the condition is at the end.
Procedures and Functions
Making use of procedures and functions is also a way of making your program effective. But what are they?
A procedure is a block of code that performs a specific task or action. It does its job and then stops, and it can be used as many times as required throughout the program. Think of it like a teacher calling out instructions to the class.
PROCEDURE greetStudent(studentName)
OUTPUT "Good morning, " + studentName
ENDPROCEDURE
// To use it in your main code:
CALL greetStudent("Alex")
In the above example, the name of the module is greetStudent and it outputs a string. In this piece of code a comment is also made. The procedure greetStudent is called to use it in a program/main code. Functions work the same way; however, they send some piece of code back to the main program using the keyword RETURN. It is usually used in codes where calculations are required. For example:
FUNCTION calculateFinalGrade(score, totalPossible)
percentage <- (score / totalPossible) * 100
RETURN percentage
ENDFUNCTION
// To use it in your main code:
finalScore <- calculateFinalGrade(18, 20)
Here, finalScore stores the percentage returned.
Now that the lengthy and redundant sections are complete, let us look at some important pointers.
What is the difference between variables and constants?
A variable is a storage entity that can be used throughout the program without having to write the entry again and again in the program. The value of a variable can change but for a constant it remains the same.
Datatypes and Operators
Datatypes are the identity of a variable or constant. Datatypes are initialised at the start of the program to keep track of its use. For example:
The variable, student, stores the count of the loop, therefore its datatype is integer. Look at the following table.
| INTEGER | numbers |
| REAL | decimals |
| BOOLEAN | yes or no |
| STRING | alphabets/other characters |
| + - * / | add, subtract, mutiply, divide |
| // | comments |
| & | concatenate |
| <- or = | assignment |
| == or = | equal to |
| <> or != | not equal to |
| > < | greater than, less than |
| >= <= | greater than equal to, less than equal to |
| DIV | divide but give quotient |
| MOD | divide but give remainder |
| AND | include both conditions |
| OR | include either condition |
Wrap Up: Practice Makes Perfect
Pseudocode might feel a bit like learning a language that doesn't actually exist, but it is the ultimate cheat code for programmers. By mastering these operators and loops, you can map out the logic of a massive app or a complex game before you ever write a single line of actual code.
Next time you sit down to build a project, put your code editor away for the first ten minutes. Grab a notebook, sketch it out in pseudocode first, and watch how much faster you build. Happy coding!
Comments