Pseudocode in Programming

Pseudocdoe and Algorithm | Pscode

Pseudocode  in Programming

In this article, I will cover the following points.

  • What is pseudocode ?
  • Why we have to study about pseudocode ?
  • What is difference between algorithm and pseudocode ?
  • Terminologies present in pseudocode ?
  • Example

Pseudo - pretended and not real

Pseudo Code | Pscode

Pseudocode is often used in all various fields of programming, whether it be mobile/app development, web development. Pseudocode is a technique used to describe the distinct steps of an algorithm in a manner that is easy to understand for anyone with basic programming knowledge.

  • Unreal or not real.
  • not same as working code.
  • plain language description of an algorithm.

If you take a problem 'P' then this will be best way to code.

'P' ---> Write Algorithm ---> Write pscode ---> Write a code

Differnece between algorithm and pseudocode

algo.jpg

Algorithm

  • Sequence of steps (finite & unambigious)
  • Written in natural language

Pseudocode

  • plain language description of algorithm
  • written in high level programming language structure

Note - I have problem 'P'. If you and me have written the pseudocode i.e. 'ps1' and 'ps2'. Depending upon the programmer choice, he/she can choose any of them. That does not mean that other pseudocode is wrong, both of pseudocode is correct it's a matter of choice and understanding.

Terminology

  • input
  • output
  • let
  • if
  • end if
  • else
  • else if
  • for
  • while
  • function/procedure
  • :=
  • <-

pseudocode cons.png

Examples

To explain the difference between algorithm and pseudocode, I will write the algorithm and pseudocode of problem.

Problem -1

Write a pseudocode for swapping two number using extra variable.

swap.gif

Algorithm

START
  Step 1: Declare a variable a, b and temp as integer
  Step 2: Read two number a and b
  Step 3: temp <- a
  Step 4: a <- b
  Step 5: b <- temp
  Step 6: print a, b
END

Pseudocode

let a, b, temp : integer
input a, b
   temp <-a
   a <- b
   b <- temp
output(a,b)

Problem-2

Write a pseudocode for checking given number is prime or not.

prime.gif

Algorithm

START
   Step 1: Declare a variable n and i as integers
   Step 2: Divide the variable n with [i =2 to n-1]
   Step 3: If n is divisible by any value of i,
                     then print number is not a prime
   Step 4: else
                     print number is prime
END

Pseudocode

function isPrime(n : integer)
    let i: integer
    for i=2 to n-1
          if (n % i==0)
              output("not a prime")
               return
          end if
    end for
    output("prime")

Problem -3

Write a pseudocode for sum of all elements in an array.

sum.png

Algorithm

START
   Step 1: take an array A and define it's value
   Step 2: loop for each value of array A 
   Step 3: take a integer variable sum=0
                   add each element to sum
   Step 4: After the loop is over, print the sum value 
END

Pseudocode

A : array of integer
function sum_array(A)
      sum <- 0
      for i=0 to A.len()-1
            sum <- sum + A[i]
      end for
      output(sum)

Summary of Pseudocode

ps.png

you might think it’s a waste of time, “why write “code” twice?”.

That might be correct in the case of simple, straightforward problems. However, as the complexity and the size of the problem increase, you will start to realize how generating pseudocode makes writing the actual code much easier. It helps you realize possible problems or design flaws in the algorithm earlier in the development stage.

Hence, saving more time and effort on fixing bugs and avoiding errors. Moreover, pseudocode allowed programmers to communicate more efficiently with others from different backgrounds, as it delivers the algorithm's idea without the complexity of syntax restrictions.

A clear, concise, straightforward pseudocode can make a big difference in the road from idea to implementation, a smooth ride for the programmer. It’s one of the overall tools underestimated by the programming community but definitely, needs to be utilized more.