Pseudocode
A. Declarative Sentences
B. Definition of Pseudocode
C. How to Write Pseudocode
Even though there are no strict regulations for writing pseudocode, some common guidelines are usually followed:
-
Write in a simple and understandable language. Pseudocode often starts with "Start" and ends with "End."
-
Use the notation ← to assign a value to a variable, for example:
distance ← 10time ← 20speed ← distance/timeacceleration ← speed/time
Everything on the right side of ← is assigned to the variable on the left (distance, time, speed, and acceleration).
-
Each statement or instruction can be independent and written on its own line, e.g.:
speed ← 10/20
This assigns the result of 10 divided by 20 to the variable speed.
-
It is recommended (though not mandatory) to use lowercase letters for scalar variables, which store changeable values, e.g.:
v ← s/tVariable
vcan change depending on the values ofsandt.
-
It is also recommended (though not mandatory) to use uppercase or capitalized names for array variables, e.g.:
Array ← [6, 7, 8, 9, 10]L ← [2, 3, 4]Array ← [6, 5, 9, 9]Here, variable
Lholds three numbers: 2, 3, and 4.
-
Notation like
L[i]refers to the i-th element in array L. Arrays typically start indexing from 0, soL[0]is the first element. For two-dimensional arrays,L[i, j]refers to the element in row i and column j, e.g.,L[0,1]means row 0, column 1. -
numberOfElements(L)is used as an expression to retrieve the number of elements in an array. -
Compound variables are used to store multiple types of data together, e.g.:
vehicle = GROUPnametypeinventorEND-GROUPTo assign values to compound variables, use ←, for example:
name ← "Bicycle"type ← "Mountain Bike"inventor ← "Baron Karl Von Drais"
-
Usually, each line (statement or comment) is numbered. For pseudocode containing sub-blocks, indentation is used, e.g., with an if statement:
1. ...2. If (speed > 50) then
2.1 statement-12.2 statement-22.3 statement-3
3. end-if4. ...
-
Pseudocode should be read sequentially. If a step needs to jump to another step due to certain conditions, it should be clearly stated, e.g., from step 2.2 "go to step 3."
-
Symbols
//or/*...*/are used for comments, e.g.:
// commentor
/* multi-line commentline-xline-x*/
-
Each instruction line should be clear, specifying the data type if needed, e.g.:
Float speed, acceleration
-
Input and output operations are represented by notations like input(), print(), display(), etc., e.g.:
Input(distance)Input(time)Print()These notations can vary depending on the programming language associated with the pseudocode.
-
For logic operations:
-
Comparison operators:•
<(Less than)•<=(Less than or equal to)•>(Greater than)•>=(Greater than or equal to)•<>or!=(Not equal) -
Logical operators:•
AND(and)•OR(or)
-
Pseudocode for while-loops:
WHILE (condition)statement-1statement-2...END-WHILEThe loop continues as long as the condition is true.
-
Pseudocode for do-loops:
DOstatement-1statement-2...WHILE (condition)The loop executes at least once before checking the condition.
-
Pseudocode for for-loops:
FOR (variable = 1 to 10)statement-1statement-2...END-FORThe loop repeats until the variable reaches 10.
-
For procedures/functions/methods:
Procedure procedure_name(parameter)statement-1statement-2...End-ProcedureThis also applies to functions, methods, classes, and subclasses.
Let's now look at an example of pseudocode and its equivalent in Java programming language for finding speed and acceleration.
The pseudocode above is an ideal example—very straightforward and clear in its line-by-line presentation. It starts with the naming of the process (in Java, this would involve creating a class named MencariKecepatandanPercepatan), the variables used are clearly listed along with their data types, the formulations for speed and acceleration are explicitly stated, and the final stage involves displaying the results. Each step tells a story and provides highly informative instructions, particularly for programmers, as they immediately understand what needs to be done without having to dig deeper for information about variables, formulations, or whether the final results should be printed. If the above algorithm is transformed into Java code, the result would be as follows:
Compare it with the non-ideal pseudocode below:
Let's correct the non-ideal pseudocode step by step, as follows:
-
Line 1 and 2: There is no issue, as they are sufficiently informative.
-
Line 3: The programmer might create variables and data types, anticipating a float data type if there is a division result.
-
Line 4: This raises a question: how many variables need to be created for initialization? The programmer could create 1, 2, 5, or 10 variables, and then initialize them with arbitrary values. The programmer will have to think about how many variables and what data types to use.
-
Line 5 and 6: The programmer will be very confused. What is the formulation for speed and acceleration? Is it true that all programmers know the formulas for speed and acceleration in physics? Of course not! This means the programmer still needs additional information.
-
Line 7 and 8: The values for speed and acceleration will exist if lines 5 and 6 are resolved.
-
Line 9: This is just the closing of the process.
Looking at the steps in the non-ideal pseudocode above raises a big question: What variables are needed to find speed and acceleration, and what are their data types? How can speed and acceleration be calculated if we do not know the formulas? This indicates that the pseudocode is not detailed enough. Taking the initiative to declare variables will never solve the confusion and deadlock that exist.
D. Pseudocode Structure
When looking at pseudocode from books, the internet, or analysts, various presentation styles are found. Some use numbering, others do not. Some have statements that resemble a specific programming language, while others only include the process itself. Some provide complete details, such as the data type of the variables, while others mix English with local languages, and there are many other variations. We must accept the fact that it is difficult to apply a specific standard if we are already following a particular programming language and considering the different backgrounds of the analysts.
Therefore, pseudocode should be created to approach a standard that is easy for programmers to understand. Of course, we cannot impose a rigid structure, as the characteristics of each available programming language have some differences. No programming language is exactly the same. Some languages are similar, for example, Java and Pascal. Essentially, these two programming languages have different characteristics. For instance, in declaring variables, Pascal declares variables outside the program block (begin…end;), while Java allows variable declaration anywhere, even inside the program block ({…}). The essence of pseudocode is that it must be understandable by both programmers and non-programmers without needing to study specific concepts.
In general, it can be said that pseudocode can be structured as follows: starting with a Title (Program Name, Class), Description (Declaration), Implementation (Core of the Algorithm).
E. Differences in Writing Style (Creation) of Pseudocode
Pseudocode is very close to English prose, such as the terms "while," "if," "else," and so on. However, we are still allowed to make some tolerances as long as it is feasible and the general audience can understand the intent and purpose of the statement. The style of pseudocode greatly depends on personal preference and the problem to be solved. The more you write/create pseudocode, the more you will discover what style is most natural and most suitable for you.
F. Examples of Algorithms with Pseudocode
As enrichment material, here is an example of pseudocode. The algorithm to find odd numbers from 1 to 10. In the following pseudocode, the task is to find the odd numbers from 1 to 10, which would be 1, 3, 5, 7, and 9. The pseudocode is as follows:
The pseudocode above, although it does not have a high level of complexity in the solution, already has an identity with 3 levels of depth, namely: 8.1.2.1, 8.1.2.2, and 8.1.2.3. If such an identity feels too long, it can be abbreviated as 8121, 8122, and 8123, or if it still feels too long, it can be replaced with letters, for example:
The two pseudocode examples above will be very easy for programmers to read systematically, and if the pseudocode is transformed into Java programming language, the result will be as follows:
That’s all for this article. Apologies for any mistakes in the wording. If there’s anything unclear, please feel free to ask in the comments section.
- KANA, ALGORITHMS and PROGRAMMING, 2024
.jpeg)











No comments:
Post a Comment