Algorithm Schema and Pseudocode for Representing Algorithms by Geometry and Syntax and Threading Structures in Programming
- 24-07-2022
- Toanngo92
- 0 Comments
Mục lục
The concept of pseudo code (pseudo code):
This is a concept of source code but not the real source code of any programming language at all, for ease of visualization, we will understand as follows: Imagine in life, with the actual language a word please. hello in Vietnamese can be translated into different languages for example English is hello, French is bonjour …., the same to describe a greeting. In programming too, each programming language will understand and compile the same job, but there are syntactical differences depending on the characteristics and structure of each language. Therefore, when approaching programming, we can use the concept of pseudocode to generally describe a work content in a coding style but more concise and easy to understand. And this language is not compiled by computer compilers, but mainly for communication between programmers. (In fact, when exchanging programming expertise, programmers also rarely write pseudocode to communicate, but mainly talk to describe and understand the problem, but in some cases the problem is complicated. or for new programmers, the pseudocode to describe the problem also applies).
Those of you who have a bit of programming skills or have access to a few programming languages may not need to refer to this article.
The most basic example of writing pseudocode:
BEGIN DISPLAY "hello world" END
This is a basic example of giving commands to the computer, but with the style of using pseudo code, this writing will be short, easy to understand and the programmer's task will be based on the syntax of the code. each specific programming language to write the command line requires the computer to print out the words "hello world" .
The running (sequentially executed) flow of the pseudocode:
- BEGIN : start the program
- DISPLAY "hello world" : print the text "hello world" to the screen
- END : End of program
The keywords of the pseudocode can be changed flexibly according to each person's taste, but we can understand START or BEGIN or even Vietnameseize it as BATDAU which is also a pseudocode to represent the syntax to start the chapter. only program. The same goes for END/STOP/KETTHUC, and this pair of keywords often goes together because in programming, there must be an end with a beginning.
Some common keywords when representing pseudocode:
- BEGIN/START => Start the program
- END/STOP => End of program
- INPUT => Requires user input and saves it to memory (keyboard interaction)
- DISPLAY/WRITE => Print to the screen
- IF/ELSE => If/then used to fork the running thread (sequentially executing the program based on a calculated condition)
- BEGIN LOOP/END LOOP => start and end of loop (a concept in programming that will be covered later in the article)
Basic example of pseudocode with a problem that calculates the sum of 2 numbers entered by the user:
BEGIN INPUT A INPUT B C = A + B DISPLAY C END
The above pseudocode tells the computer to execute the jobs sequentially:
- Start the program
- Ask the user to enter 2 numbers and name 2 storage resources A, B to store the user input value,
- Also need a resource C, calculated by the sum of A and B
- then display the value of the resource named C to the screen.
- End program
The concept of algorithm diagram (algorithm diagram):
This is a form of algorithm representation in geometric form, when reading the algorithm diagram, the programmer will visualize the flow of the program, and each action in the execution process will be represented by a symbol. corresponding.
The table describes the symbols in the algorithm schema :
Symbol | Description of the concept |
Program start/end (Terminator) | |
Input, output/print to the screen (Input/Output) | |
Processing steps | |
Decision to branch program according to condition (Decision) | |
In-page connection, used to connect to another schema element when the schema is too long or complex (On-page connector) | |
Off-page connector, used to connect to another schema element when the schema is too long or complex (Off-page connector) |
Here is the algorithm diagram with the problem of calculating the sum of 2 numbers entered by the user:
The above algorithmic scheme requires the computer to execute sequentially the following tasks:
- Start the program (Terminator)
- Ask the user to enter 2 numbers and name 2 storage resources A, B to store the user input value. (Input)
- Also need a resource C, calculated by the sum of A and B (Processing)
- then display the value of the resource named C to the screen. (Output)
- Terminator (Terminator)
Advanced threading constructs in programming
As described above, the program runs in a sequential structure, running will go from the top down, a little more advanced, let's try to imagine:
- Solve the problem of checking whether the user's input data is even or odd .
- Solve the problem of printing 1000 times a content "dong 1', 'dong 2' … 'dong 100' to the screen.
With these problems, we will need to approach some more advanced structural concepts in programming, this structure applies to almost all programming languages, and programmers know it by heart.
To solve problem number 1, we use the IF (if) structure, we can imagine like reality if the condition is satisfied, then do something.
Structure IF (if), IF – ELSE (if then – otherwise), IF – ELSE IF – ELSE (split program flow into multiple branches based on condition)
The IF structure always comes with the same test condition, the computer will rely on the test condition to fork the program according to the programmer's intentions. If the condition is true, the thread running the program will jump into the statements of the body inside the IF structure to execute, the program will identify the body with the ENDIF keyword. An IF structure must always end with the ENDIF . keyword
The first pseudocode uses the IF structure for the problem of checking whether the user inputted data is even or odd :
BEGIN DISPLAY "Nhap vao so can kiem tra" INPUT A RES = A/2 IF (R == 0) DISPLAY "So vua nhap vao la so chan" ENDIF END
The pseudocode is more compact when directly checking the expression A MOD 2 is zero or not, no need to pass an RES resource to store the value
BEGIN DISPLAY "Nhap vao so can kiem tra" INPUT A IF (A MOD 2 == 0) DISPLAY "So vua nhap vao la so chan" ENDIF END
IF ELSE structure (if – then / otherwise – then)
In the above pseudocode example, we see that if the condition is not satisfied, the program will end without printing the line "So staggered", to handle this problem, if only IF is used then pseudocode would be:
BEGIN DISPLAY "Nhap vao so can kiem tra" INPUT A IF (A MOD 2 == 0) DISPLAY "So vua nhap vao la so chan" ENDIF IF (A MOD 2 !=0) DISPLAY "So vua nhap vao la so le" ENDIF END
However, we can use the solution of adding the ELSE keyword inside the IF structure to fork the program when the result of the division is non-zero, the pseudocode is as follows:
BEGIN DISPLAY "Nhap vao so can kiem tra" INPUT A IF (A MOD 2 == 0) DISPLAY "So vua nhap vao la so chan" ELSE DISPLAY "So vua nhap vao la so le" ENDIF END
In addition, with this situation we can completely do the IF structure as follows, the output is still the same.
BEGIN DISPLAY "Nhap vao so can kiem tra" INPUT A IF (A MOD 2 != 0) DISPLAY "So vua nhap vao la so le" ELSE DISPLAY "So vua nhap vao la so chan" ENDIF END
With the IF ELSE structure, the pseudocode will be somewhat concise and easy to read and understand, and from a logical perspective, the way to do IF above the program must once again check for non-zero conditions, and with IF ELSE , we only need to check once, when the condition is not satisfied the program will immediately fork into the body block inside the else , and usually, programmers choose this way.
Algorithm diagram for the above problem:
Structure IF ELSE IF ELSE
With this structure, you can fork the program into more than 2 threads that run according to the program condition, refer to the following pseudocode:
BEGIN IF ( DIEUKIEN1 ) // LAM GI DO ELSEIF ( DIEUKIEN2) // LAM GI DO ELSE // TRUONG HOP CON LAI ENDIF END
AND/OR structure (check multiple conditions at the same time)
We have just approached the IF structure in the previous section, but only used it for the problem of testing 1 condition that is the result of division and remainder equal to 0 or not, in this introduction, we come to the conceptual solution. concept of using AND/OR to check multiple conditions in the program at the same time.
Example computer pseudocode that solves the following problem to explain the AND structure:
Test a student entered by user including name, age, grade, based on the scale to rank. The score scale is as follows:
- Grade A: 8<=points<=10
- Grade B: 6<=points<=8
- Grade C: <6
BEGIN DISPLAY "NHAP VAO SINH VIEN" DISPLAY "NHAP VAO TEN"; INPUT NAME DISPLAY "NHAP VAO TUOI"; INPUT AGE DISPLAY "NHAP VAO DIEM"; INPUT MARK IF (MARK >= 8 AND MARK <= 10) DISPLAY "SINH VIEN " + NAME + " XEP HANG A" ELSE IF(MARK >= 6 AND MARK <=8) DISPLAY "SINH VIEN " + NAME + " XEP HANG B" ELSE DISPLAY "SINH VIEN " + NAME + " XEP HANG C" ENDIF END
Example computer pseudocode that solves the following problem to explain the OR structure:
Check the user entered student data entered by the user including name, age, check if the user entered age, if greater than 0 and less than 18 then the printout is invalid
BEGIN DISPLAY "NHAP VAO SINH VIEN" DISPLAY "NHAP VAO TEN"; INPUT NAME DISPLAY "NHAP VAO TUOI"; INPUT AGE IF(AGE < 0 OR AGE > 18) DISPLAY "TUOI NHAP VAO KHONG HOP LE" ENDIF // DO SOMETHING END
NESTED IF (nested IF) structure
Example pseudocode that solves the problem of nested IF structures
Solve the problem of checking user-entered student data including name, age and grades,
Step 1: if the name is not satisfied, print out the name and end
Step 2: if the name matches, ask to enter the age if the unsatisfactory age is greater than 0 and less than 18, it will print to the screen invalid and end
Step 3: if satisfied, then ask to enter the score, continue to check the score to rank
- Grade A: 8<=points<=10
- Grade B: 6<=points<=8
- Grade C: <6
BEGIN DISPLAY "NHAP VAO TEN SINH VIEN" INPUT NAME IF ( COUNTCHARACTERS(NAME) <= 0 ) DISPLAY "NHAP VAO TUOI SINH VIEN" INPUT AGE IF (AGE < 0 OR AGE > 18) DISPLAY "TUOI KHONG THOA MAN YEU CAU" ELSE INPUT MARK IF(MARK >= 8 AND MARK =< 10) DISPLAY "SINH VIEN XEP HANG A" ELSE IF(MARK >= 6 And MARK <= 8) DISPLAY "SINH VIEN XEP HANG B" ELSE DISPLAY "SINH VIEN XEP HANG C" ENDIF ENDIF ELSE DISPLAY "CHUA NHAP TEN SINH VIEN" ENDIF END
LOOP (loop) structure
As the structures we have learned above, here we can understand the basic computer program that will execute the instructions sequentially, more advanced, to make the program more powerful, the concept of loop provide a solution that does the same job over and over again. It is easier to say that telling the computer to execute continuously and repeat a job until we give a stop signal, in this context we use the result of the condition test as the signal above.
Consider the above problem pseudocode with the solution using loops for better understanding. With the above problem, when the user enters the data, we should ask the user to re-enter when the user input is not satisfied, but should not interrupt the program, the pseudocode shows as follows:
BEGIN NAME = "" WHILE (COUNTCHARACTERS(NAME) <= 0) DO LOOP DISPLAY "NHAP VAO TEN SINH VIEN" INPUT NAME ENDLOOP AGE = 0 WHILE (AGE < 0 OR AGE > 18) DO LOOP DISPLAY "NHAP VAO TUOI SINH VIEN" INPUT AGE ENDLOOP // THUC THI NHAP VAO VA KIEM TRA DIEM END
Here is a summary of the threading structures running in web888 programming to introduce to readers, if you feel good, please share it for the author, thank you for reading, wish you a good study!