Introduction of Pseudocode

k13i

konnichiwa sekai

Posted on August 11, 2022

Introduction of Pseudocode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ 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
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ 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
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ 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
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ WHILE

count = 0
WHILE count < 10
  ADD 1 to count
  WRITE count
ENDWHILE
WRITE โ€œThe endโ€
Enter fullscreen mode Exit fullscreen mode

You can separate sequence into modules

count = 0
WHILE count < 10
   DO PROCESS
ENDWHILE
WRITE โ€œThe endโ€

PROCESS
ADD 1 to count
WRITE count
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ REPEAT/UNTIL

count = 0
REPEAT
  ADD 1 to count
  WRITE count
UNTIL count >= 10
WRITE โ€œThe endโ€
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ FOR

FOR x = 1 to 10
  IF x % 2 = 0
    WRITE x + " is even"
  ELSE
    WRITE x + " is odd"
ENDFOR
Enter fullscreen mode Exit fullscreen mode


๐Ÿพ 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
Enter fullscreen mode Exit fullscreen mode


๐Ÿ” 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

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
k13i
konnichiwa sekai

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

ยฉ TheLazy.dev

About