A pseudocode is a set of instructions that results from the interpretation of an algorithm, which will then be transformed into a more formal code that will be written in a programming language. This is a kind of basic code, but very detailed.
They should not be confused with algorithms , since the latter are written as a recipe, with a beginning and an end, without detailing more aspects of what the final program will be. While the pseudocodes are more focused on the problem and highlight what needs to be done and that will later be taken into a programming language, such as Java, PHP, etc.
Pseudocodes can be used to model any program. No matter what language it ends up coding under, the goal of this structure is to establish how we will do it and what we will achieve.
Examples of pseudocodes
- Example 1: Pseudocode that provides the volume of a cylinder, knowing its height and diameter.
Show “Enter the diameter, in meters”: Ask for D
Show “Enter the height, in meters”: Ask for H
R = D / 2: Pi = 3.141593
V = Pi * (R ^ 2) * H
Show “The volume of the cylinder is”, V, “cubic meters”
- Example 2: Program that lets you know if a number is greater, less than or equal to zero.
Write “Enter a number”
IF NUMBER> 0 THEN
write “The number entered is positive”
IF NUMBER <0 THEN
write “The entered number is negative”
write “The number is zero”
- Examples 3: The user must enter two numbers and the sum of both will be displayed.
INTEGER Number1, Number2, Result;
WRITING («Tell me two numbers to add:«);
READ (Number1, Number2);
Result <- Number1 + Number2;
WRITE («The sum is:«, Result);
- Example 4: Program that asks for a number and writes its square
INTEGER nNumber, nSquare;
WRITE («Tell me a number»);
nSquare <- SQR (nNumber);
- Example 5: Program that says the multiplication table of the number entered by the user.
Program: Multiply Table
numerical: t, num, total;
Write «Enter a number»
From t = 1 to t = 10 repeat
make total = num * t
- Example 6: Pseudocode that provides the average price of a product, calculated from its price in three different stores.
Show “Enter the price of the product in the establishment number 1, in
euros ”: Ask for Price1
Show “Enter the price of the product in establishment number 2, in
euros ”: Ask for Price2
Show “Enter the price of the product in establishment number 3, in
euros ”: Request Price3
Average = (Price1 + Price2 + Price3) / 3
Show “The average price of the product is”, Average, “Euros”