DEV Community

Zachary Hadjah
Zachary Hadjah

Posted on

How to understand pseudocode

Back in freshman year during undergrad, I remember being in my professor's office hours asking for help with an algorithm. I still don't remember what the algorithm was, but what I recall was how I didn't understand how I was supposed to properly format and code the algorithm. All of this was preparation for an exam. I asked him how I was supposed to do all of this during a test in which I would have to write it by hand. His response sticks with me to this day, "Never memorize anything. Understand how tools work, understand the order of operations, and go from there."

Pseudocode is a great way of using English in order to understand code.

      Alloc mem for int userInput;
      If(userInput > 5){
        OUTPUT("Larger than 5");
      }
Enter fullscreen mode Exit fullscreen mode

The above line of pseudocode would never compile on any ide in any language, but would be easy for humans to simply understand.

Allocate memory for an integer variable.
-If that integer variable is larger than 5
-Print to the console

In terms of pseudocode "syntax," there really isn't any method of writing it properly. It's mostly just minimal language that allows the programmer to understand exactly how to format the code:
-What order each operation needs to be placed in
-Where loops or logic statements need to be placed
-How to structure algorithms
-Where to place variables for certain formulas

Recently I created a mini-site in which I implemented pseudocode in order to help me better construct the logic behind the classic FizzBuzz problem. The version of FizzBuzz I completed takes two numbers below 100 as input. It will print fizz for all the numbers that are divisible by the first number, and buzz for all the numbers that are divisible by the second. It will print FizzBuzz for all numbers divisible by both numbers.

//loop over every number between 1 and 100
For(variable being accessed){

//print fizzbuzz if the number is divisible by both user inputs
If((varElement % fizzNum)== 0 AND (varElement % buzzNum)== 0)
OUTPUT("FizzBuzz")

//print the number if the number isn't divisible by both user
//inputs
If((varElement % fizzNum)!= 0 AND (varElement % buzzNum)!= 0)
OUTPUT( var )

//print fizz if the number is divisible
If((var % fizzNum)==0)
OUTPUT("Fizz")

//print buzz if the number is divisible by the buzz number
If(((var % buzzNum)== 0)
OUTPUT("Buzz")
}

Of course, you would have to have an understanding of the mod operator and equality operator to understand the code above, but the pseudocode mixed with the comments are enough to get a basic understanding of the algorithm.

Understand the tools you have to work with, the problem needed to be solved, then use pseudocode to format the algorithm.

Top comments (0)