Pseudo Code Standard
This standard was generated with the help of OpenAI (ChatGPT) 4.0.
Syntax Overview
The following syntax is designed to mimic a sort of "baby" programming language. Instead of being plain English, it has a bit syntax and structure to it, whilst also not restricting you in anyway. In fact, you can completely ignore things like the case of words (i.e. if instead of IF) or using the END keyword. The chocie is yours.
The important thing to remember is this: do whatever makes it easier for you to bridge the gap between a real-world (or abstract) concept and the code needed to make a program to solve it.
Comments
You will need to leave comments to future you and other coders that will be reading your code, even pseudo code that you might never share. Comments are a good way of explaining some complex code you're written or just as a way of thinking out loud and noting it down.
Comments in a real language, like Python, are ignored by the language itself.
Example
// Increment counter by 1
SET counter TO counter + 1
// I forgot why I wrote this
IF (110 < 67.8) OR (("a" == "b") OR (110 > 5) AND true)
PRINT "hello"
EXIT
Operations
These are actions or tests you can take or use on a variable or some value.
a == b- isaequal (==) toba != b- isanot equal (!=) toba > b- isagreater (>) thanba >= b- isagreater (>) than or equal (=) toba < b- isaless (<) thanba <= b- isaless (<) than or equal (=) tob
Examples
For testing numbers:
10 == 10 // true
10 != 11 // true
8 != 8 // false (8 IS equal to 8)
10 > 9 // false
10 > 4 // true
100 <= 99 // false
And for string values:
Tests like <, >, <=, etc., don't make sense on strings unless you're testing something numeric, like their length.
Variable Assignment
Variables allow us to store values for use later on. We can also manipulate them in various ways, too.
If you don't know the value, or you want to accept a value from the user, then you can do this:
Where ? means, "I don't know yet.", because there will be plenty of occasions in your programming future when you'll want some value that won't be known ahead of time.
Examples
IF Statement
Used to "branch" and create a flow chart like structure in your code. You will use a lot of IF statements in a complex and/or large code base.
IF condition THEN
// Statements to execute if condition is true
ELSE
// Statements to execute if condition is false (Optional)
ENDIF
Example
SET result TO "Unknown"
IF x > 10 THEN
SET result TO "Greater"
ELSE
SET result TO "Smaller or Equal"
ENDIF
WHILE Loop
Looping over a range of numbers, or a list of "things", is equally as common as the IF statement. Essentially, you'll collect items into a list and then you'll need to loop over that list and perform some operation on the items.
The condition is some test of truthness. Like 1 < 100, which is true, until it's not true.
Example
Because counter is initially set to 1 and we increment it by 1 each time the loop "loops", the output will look like this:
"Hello, world" // counter = 1
"Hello, world" // counter = 2
"Hello, world" // counter = 3
"Hello, world" // counter = 4
Note how only four lines print out, and not five? That's because eventually counter == 5, and 5 is NOT less than 5, it's equal to it. To get fives lines you can do this:
SET counter TO 1
WHILE counter < 6 DO // notice the change here, from 5 to 6
PRINT "Hello, world"
SET counter TO counter + 1
ENDWHILE
So the output would be:
"Hello, world" // counter = 1
"Hello, world" // counter = 2
"Hello, world" // counter = 3
"Hello, world" // counter = 4
"Hello, world" // counter = 5
FOR Loop
A FOR loop has the same objectives as the WHILE loop, but allows us to solve the same problem in a different way.
FOR variable FROM start_value TO end_value DO
// Statements to execute for each value from start to end, inclusive
ENDFOR
So instead of looping because a condition is true in a WHILE loop, we use a FOR loop to go between two values whilst also consuming the current value in the range.
Example
Full Example
Here's how a simple program using this pseudo code language might look:
SET counter TO 0
SET sum TO 0
SET i TO 1
WHILE i != 100 DO
SET sum TO sum + i
SET i TO i + 1
ENDFOR
IF sum > 5000 THEN
SET result TO "Sum is large"
ELSE
SET result TO "Sum is within range"
ENDIF
WHILE counter < 5 DO
SET counter TO counter + 1
// Print or log 'counter' value here if desired
ENDWHILE
This pseudo code initializes a counter and a sum variable, then uses a FOR loop to sum up numbers from 1 to 100. It checks if the sum is greater than 5000 using an IF statement, and uses a WHILE loop to increment the counter until it reaches 5.