Introduction of Pseudocode
konnichiwa sekai
Posted on August 11, 2022
How do often you write code? In my case, I often write code thinking logic in my head. However, it is inefficient practice especially when the code has complicated logic. How do you explain the logic to someone who has different programing language background? You might be distracted by the difference of each language syntax. That is the reason why I started searching technique to create a draft of code before writing and found pseudo code. So, I want to output what I have learned in this article.
Just so there's no confusion,
There is no universal "standard" for this code. Each textbook may have their own personal style of notation. Pseudocode is not a rigorous notation, since it is read by other people, not by the computer. Please read this article only as a guide.
โ What Is Pseudo Code?
It is a simpler version of programming code in plain English before it is implemented in a specific programming language.
You can use it to create outline of the code you want to write.
๐ Advantage
- Pseudocode is understood by the programmers of all types of programming language.
- It enables the programmer to concentrate only on the algorithm part of the code development while writing.
- You can save time for coding as conceptual logic have already been fixed.
โ ๏ธ Disadvantage
- It doesn't visualize logic like flowchart does.
- Notation varies widely depending on the writer.
๐ Main structures in pseudo code
- SEQUENCE represents linear tasks sequentially performed one after the other.
- SELECTION performs actions based on the condition given
- ITERATION allows for multiple execution of statement
๐ Tips for clear pseudo code
- Write only one task per line
- Capitalize initial keyword
- Indent to show hierarchy
- End multiline structures
- Keep your statements language-independent
๐พ 1. Write only one task per line
You should only write one task for one line to make your pseudo code more readable. Here is example code below.
EX_BEFORE
Read name, product_price, amount, tax_rate
sales = product_price * amount, tax = sales * tax_rate
Write name, sales, sales + tax
EX_AFTER
READ name, product price, amount, tax_rate
sales = product_price * amount
tax = sales * tax_rate
sales_with_tax = sales + tax
WRITE name, sales, sales_with_tax
๐พ 2. Capitalize initial keyword
In the example above, READ and WRITE are in caps. Keywords below are examples you should write in caps.
READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL
๐พ 3. Indent to show hierarchy
We will use a particular indentation pattern in each of the design structures:
- SEQUENCE : keep statements in sequence all starting in the same column.
- SELECTION : indent the statements inside the selection structure, but not the keywords that form the selection
- ITERATION : indent the statements inside the loop, but not the keywords that form the loop
EX
READ name, result
IF result >= 80
message = "You passed"
ELSE
message = "You failed"
ENDIF
WRITE message
๐พ 4. End multiline structures
See how IF statement ends with ENDIF in the above example. Regardless of type of keyword, whatever starts the structure shoud end with END-KEYWORD. For example, WHILE keyword ends with ENDWHILE and so on.
๐พ 5. Keep your statements language-independent
Avoid using the special features available in the language you plan to write the program in as much as you can. If you are SURE it will be written in that language, then you can use the features. However, if not, it may cost you extra time or work.
๐ก Examples of pseudo code
๐พ If statement
READ name, result
IF result = 100
message = "Perfect!"
ELSEIF result >= 80
message = "You passed"
ELSE
message = "You failed"
ENDIF
WRITE message
๐พ Nested if
READ name,sex,is_married
IF sex = male
WRITE "Hello Mr. " + name
ELSE
IF is_married = true
WRITE "Hello Mrs. " + name
ELSE
WRITE "Hello Ms. " + name
ENDIF
ENDIF
๐พ WHILE
count = 0
WHILE count < 10
ADD 1 to count
WRITE count
ENDWHILE
WRITE โThe endโ
You can separate sequence into modules
count = 0
WHILE count < 10
DO PROCESS
ENDWHILE
WRITE โThe endโ
PROCESS
ADD 1 to count
WRITE count
๐พ REPEAT/UNTIL
count = 0
REPEAT
ADD 1 to count
WRITE count
UNTIL count >= 10
WRITE โThe endโ
๐พ FOR
FOR x = 1 to 10
IF x % 2 = 0
WRITE x + " is even"
ELSE
WRITE x + " is odd"
ENDFOR
๐พ CASE
CASE Day OF
1 : WRITE "Monday"
2 : WRITE "Tuesday"
3 : WRITE "Wednesday"
4 : WRITE "Thursday"
5 : WRITE "Friday"
6 : WRITE "Saturday"
7 : WRITE "Sunday"
OTHERWISE OUTPUT "Day invalid"
ENDCASE
๐ In the end
The techniques I introduced may not be the best practice. You have to figure out your own notation. But, I hope that this article helps you understand pseudo code and start using it.
๐ Reference
PseudocodeBasics
Pseudocode: What It Is and How to Write It
Types of Logic structure
Posted on August 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 4, 2024
September 11, 2024
September 24, 2024