DEV Community


Posted on

Explain Pseudocode

Okay. I've just started learning Python, and I'm finding it way easier to understand than pseudocode. This post is less of a serious debate, and more of a search to satisfy my curiosity.

Normally I come up with an idea and jump into coding it without much planning, so before now writing pseudocode was an unknown to me. I've done a bit of googling, and the basic concept is simple enough - "pseudo" (fake) code, written in plain English, so that it can easily be converted to almost any language by developers. Makes sense.

Now, my problem is the syntax. Does it actually have any agreed upon / recommended syntax, or is it literally just write whatever? If it does have recommended syntax that the majority of devs agree on, then what would that be? Most things I've found online have maybe one or two sentences about pseudocode and are mostly about flowcharts, or seem to be in complete disagreement with each other.

Take, for example, this simple Python script that flips a coin:

# import modules
import random
import time

# flip coin
def flip():
    coin = ''
    rand = bool(random.getrandbits(1))
    if rand:
        coin = 'heads'
        coin = 'tails'
    return coin

# allow for multiple games
playagain = 'Y'
while playagain.lower() in {'y', 'yes', ''}:
    print('The coin spins in midair...')
    print('...and lands on \033[1m' + flip() + '\033[0m!')
    playagain = input('\nDo you wish to flip the coin again (Y/N)? ')
Enter fullscreen mode Exit fullscreen mode

I've attempted to write some pseudocode for this. Which one of the below would people say is better? Could anyone demonstrate how they would write pseudocode for this?

# attempt 1

set playagain to yes
while (playagain is y, yes, or an empty string (ignoring capitalisation)) do
    tell user the coins spins in midair
    wait 1 second
    run function "flip"
        set rand to a randomly generated booleen (true/false)
        if (rand is true)
            set coin to heads
        else (rand is false)
            set coin to tails
        return coin
    tell user what the result of "flip" was
    ask user if they want to play more games and set playagain to user response

# attempt 2

FUNCTION flip() =
    rand = random booleen
    if (rand)
        coin = heads
        coin = tails
    return coin

playagain = yes
while (playagain == y, yes, or an empty string (ignoring capitalisation)) do
    output "The coin spins in midair..."
    sleep 1 second
    ouput "...and lands on " flip()
    output "Do you wish to flip the coin again (Y/N)?"
    playagain = input
Enter fullscreen mode Exit fullscreen mode

This may seem ridiculous to the majority of you, but I would appreciate any help!

Discussion (4)

dmerand profile image
Donald Merand • Edited

To me, pseudocode is nice when you need to understand the algorithm you're using without getting stuck with language implementation details. In your Python examples above, you approach the algorithm in a certain way because you are secretly thinking "I need to make sure to declare my variables and use a specific looping syntax, etc." Pseudocode allows you to forget about all that and focus on the bigger picture. So you might start with:

# What is the algorithm actually doing?
until user_cancels_program
  print coin_flip_result

...and then worry more about implementation details like "How do I flip a coin?"

coin_flip_result = a_random_boolean

# Or, more explicitly;
coin_flip_result =
  let flip = get_a_random_boolean
  heads if flip is true
  tails if flip is false

You can just pretend that you're using a magic programming language that takes care of things for you! It's pretty nice, and you can easily convert that to Python or JavaScript or what-have-you, once you figure out how you want things to generally work.

denmch profile image
Den McHenry

The way I think about and have used pseudocode is all about breaking a problem down into a logical sequence of steps in a way that's easy to read. It's easy to fall into patterns and write code that works but gets there in a less clear or less elegant way, and pseudocode removes the syntax that might be guided down a certain path by focusing on what should happen rather than how it happens.

dragonwocky profile image
dragonwocky Author

Thanks! In a few words, you've explained pseudocode better than every other guide or explanation I've found - "what should happen rather than how it happens".

zeerorg profile image
Rishabh Gupta • Edited

There is no single set way to write pseudocode. Even in traditional books you'll find various different ways it has been done. CLRS has pseudocode implementation very much similar to your first attempt.

I'd say any pseudocode has mainly 2 properties:

  1. It should be easy to read
  2. It should be easy to understand what it is doing

I'd say both the attempts are correct, since there are no fixed guidelines to writing pseudocode.