Pseudocode Meaning

Pseudocode

Pseudocode Meaning

Programming languages widely used today already incorporate natural language keywords; this is different from programming languages during the early days of computers, which used machine language. Nowadays, we are free to define variable names according to established conventions, for example, variables named "speed", "height", "weight", "area". Statements in programming languages generally use English for their keywords. However, in pseudocode, each line of words or sentences can use any language the writer is proficient in.

A. Declarative Sentences

For analysts/programmers who are accustomed to using English, creating pseudocode with keywords similar to programming languages is quite easy, and this becomes an advantage for analysts/programmers from English-speaking countries. However, we should not be discouraged by this. Analysts or programmers from non-English-speaking countries, such as Indonesia, are absolutely allowed to use their native language when writing pseudocode. If some English terms appear in the examples, it is merely to help readers become familiar with commands in programming languages such as Java—for instance, terms like "if", "else", "else if", "while", "do while", "loop", etc. Words like "Add" or "Divide" can still be replaced with their local language equivalents.

B. Definition of Pseudocode

Pseudocode is a high-level and concise description intended for human reading, not machine (compiler) reading. You can think of pseudocode as "English code" or "local language code" that is understandable by anyone (not just computer scientists). Although pseudocode closely resembles actual programming language syntax, it is not bound to any particular language and can be easily converted into any high-level programming language such as Java, Pascal/Delphi, Visual Basic, or C++.

C. How to Write Pseudocode

There are no strict rules for writing pseudocode, but there are generally accepted standards. A reader should be able to follow the pseudocode and clearly understand what needs to be done, making it easier to translate the logic into actual programming steps. After writing pseudocode, a programmer should find it easier to convert it into real code.

When writing pseudocode, from the first step onward, indentation can be used for certain code blocks to show structure clearly—such as within methods, loops, etc. Indentation is very important when writing pseudocode. In programming languages, indentation might not affect how the code runs, but for humans reading it, it is crucial for understanding the logic. Pseudocode writing should not make it difficult for programmers to identify each step; therefore, numbering each step becomes important, including using sub-numbers (e.g., 6.1, 6.a) for indented blocks.
Remember: Human understanding is the essence of pseudocode.

Even though there are no strict regulations for writing pseudocode, some common guidelines are usually followed:

  1. Write in a simple and understandable language. Pseudocode often starts with "Start" and ends with "End."

  2. Use the notation ← to assign a value to a variable, for example:

distance ← 10  
time ← 20  
speed ← distance/time  
acceleration ← speed/time

Everything on the right side of ← is assigned to the variable on the left (distance, time, speed, and acceleration).

  1. 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.

  1. It is recommended (though not mandatory) to use lowercase letters for scalar variables, which store changeable values, e.g.:

v ← s/t

Variable v can change depending on the values of s and t.

  1. 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 L holds three numbers: 2, 3, and 4.

  1. Notation like L[i] refers to the i-th element in array L. Arrays typically start indexing from 0, so L[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.

  2. numberOfElements(L) is used as an expression to retrieve the number of elements in an array.

  3. Compound variables are used to store multiple types of data together, e.g.:

vehicle = GROUP  
    name  
    type  
    inventor  
END-GROUP

To assign values to compound variables, use ←, for example:

name ← "Bicycle"  
type ← "Mountain Bike"  
inventor ← "Baron Karl Von Drais"
  1. 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-1
2.2 statement-2
2.3 statement-3
3. end-if
4. ...
  1. 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."

  2. Symbols // or /*...*/ are used for comments, e.g.:

// comment

or

/* multi-line comment
line-x
line-x
*/
  1. Each instruction line should be clear, specifying the data type if needed, e.g.:

Float speed, acceleration
  1. 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.

  1. 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)

  1. Pseudocode for while-loops:

WHILE (condition)  
    statement-1  
    statement-2  
    ...  
END-WHILE

The loop continues as long as the condition is true.

  1. Pseudocode for do-loops:

DO  
    statement-1  
    statement-2  
    ...  
WHILE (condition)

The loop executes at least once before checking the condition.

  1. Pseudocode for for-loops:

FOR (variable = 1 to 10)  
    statement-1  
    statement-2  
    ...  
END-FOR

The loop repeats until the variable reaches 10.

  1. For procedures/functions/methods:

Procedure procedure_name(parameter)  
    statement-1  
    statement-2  
    ...  
End-Procedure

This 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.

Pseudocode

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:

Pseudocode

Compare it with the non-ideal pseudocode below:

Pseudocode

Let's correct the non-ideal pseudocode step by step, as follows:

  1. Line 1 and 2: There is no issue, as they are sufficiently informative.

  2. Line 3: The programmer might create variables and data types, anticipating a float data type if there is a division result.

  3. 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.

  4. 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.

  5. Line 7 and 8: The values for speed and acceleration will exist if lines 5 and 6 are resolved.

  6. 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).

Structure Pseudocode

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:

Pseudocode

For lines 1 to 7, it is very clear. In line 8, the indentation from 8.1 to 8.3 can be considered as one complete block. Line 8.2 has a sub-indentation at 8.2.1, which clearly indicates it is a sub-block within the main block.
The loop (see "While..." in line 8) forms an indentation block, with at least one indentation within its block (lines 8.1 to 8.3). Then, for the condition (see "if..." in line 8.2), it must have at least one indentation in its block.
Now, let's get into the substance of the algorithm. Why use an algorithm to find the odd numbers between 1 and 10? Sometimes, seemingly trivial questions like this arise, often undermining others' work, without considering that if the data being processed isn't just from 1 to 10 but rather up to 100, 500, 1000, or 10,000, it would be very troublesome to find each number one by one. It is very easy to find and write out the odd numbers if the data is just from 1 to 10, where the result is 1, 3, 5, 7, 9. However, it would take a long time to find the odd numbers between 1 and 1000. This is where the function of algorithms and programming languages comes in. You simply input the value, press enter, and the result appears in less than a second.
The pseudocode, when transformed into Java programming language, would look like this:

Pseudocode

Output :
Pseudocode Output Example

Algorithm for Sorting Data
You may have used MS Excel, where a column contains random data (not in order). In Excel, there is a function to sort the data called "Sort & Filter."

Algorithm for Sorting Data

The image above shows data in an Excel application. The data on the left is unsorted, while the data on the right is sorted. The "Sort & Filter" function is very easy and simple to use. Behind the ease of using the sorting function lies the sorting algorithm.
As another example, let's say we have a set of random, unsorted data, which are as follows: 3, 1, 6, 4, 6, 7, 2, 5, 6, 3. Create pseudocode to sort the data.

Create pseudocode to sort the data.

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:

Pseudocode

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:

Pseoducode Java

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.

References:
  • KANA, ALGORITHMS and PROGRAMMING, 2024

No comments:

Post a Comment