Pseudocode
Pseudocoding is an essential programming skill that you will be expected to know as you continue your career in computer science. And you can do it without a computer! Pseudocode is simplified code, written for humans to understand rather than computers. Usually we use pseudocode to produce a high-level outline of an algorithm. An algorithm is a series of steps a program takes to solve a certain problem or perform a certain task. Sometimes these steps become quite complicated, so it helps to abstract them from the actual code implementation. You can think of pseudocode as "english code". There is no standard syntax for pseudocode, and it is not language specific, which means that the same pseudocode can be easily converted to Java or Python or C++. Additionally, someone who does not know Java should be able to read your pseudocode and understand exactly what is happening.
Goal: Practice writing pseudocode and understand how pseudocode translates to real code.
Example Pseudocode
The pseudocode below describes how to print all the odd numbers between one and ten.
method boolean isOdd(int i):
if i is divisible by 2:
return false
return true
method printOddNumbers():
i = 1
while i < 11:
if isOdd(i):
print i
i++
Look carefully at the pseudocode above. There is no formal Java syntax, but you can still hand-simulate what is going to happen at each step. In fact, there really shouldn't be any syntax that is specific to Java: after writing your pseudocode, you should be able to implement the algorithm in any language you wish.
Notice also how we've formatted the pseudocode. We use indentation to delineate blocks of code, so it is clear which lines are inside which method, loop, etc. Indentation is crucial to writing pseudocode. Java may not care if you don't indent inside your if statements, but a human reader would be completely lost without indentation cues. (And the whole point of pseudocode is to be more readable for humans!)
Differences in Pseudocode Style
The pseudocode above happens to correspond line by line to actual code. This is not always true of pseudocode. Frequently there are small implementation details that we can omit in pseudocode for the sake of seeing the bigger picture. However, the more specific your pseudocode is, the more useful it will be when you have to translate it to real code. Here's another example of pseudocode that is more high level than the last:
method setupLecture():
while neuro midterm is in progress:
HTAs stop students from entering MacMillan 117
AV TAs show animation
if Andy is late:
HTAs panic
else:
AV TAs give Andy his microphone
AV TAs give Andy his tablet
Humor TAs perform water skit
This time, the pseudocode is much closer to prose english. And yet we can still see how we could translate it to object oriented code. And perhaps implementing the Humor TAs' water skit actually involves many elaborate steps, but for the sake of seeing the big picture in pseudocode, we've condensed those steps into one. Pseudocode style depends greatly on personal preference, but it also depends on what you're optimizing for: speed, readability, aesthetics, etc. As you write more pseudocode, you will discover what style comes most naturally to you. Future CS classes (such as CS16) may also ask you to follow specific pseudocode style conventions.