Sometimes, we can read the spec of a coding problem and know how to solve it right away! However, most of the time, the problem is just a bit too complicated or difficult for this to be possible. In those cases, what should we do?
Well, one helpful technique that many programmers use to solve complex problems is called pseudocode! Pseudocode is a way to break down problems into manageable steps so that we don’t have to come up with the entire solution in our minds before writing it down.
Pseudocode resembles code, but it’s not really code (it won’t compile in Java). In fact, it’s commonly written in plain English so it’s easy for us to understand. By using pseudocode, we can approach our problem step by step instead of tackling the whole thing at once, which can be very difficult.
Remember that pseudocode should always be commented out and that it’s not graded. It’s just a technique that may or may not be useful when you’re stuck on a problem.
Here are some examples of how to use pseudocode below!
Let’s say you wanted to write a program that uses a Turtle to draw a blue triangle. Pseudocode might look like:
Create a Turtle Pick a paintbrush color (blue) Draw a triangle: - Draw a line - Turn 120 degrees - Draw a second line - Turn 120 degrees - Draw a third line
Let’s say you wanted to sum up the numbers from 1 to 10, and print the result. Pseudocode could look like:
Keep track of the sum so we can eventually print it - use a variable Get numbers 1 through 10 by using a for loop Inside the for loop, add the number to the sum After the for loop, print the result
Let’s say you wanted to write a method that takes a String
s and an integer
num, and prints out
num repetitions of
Method header: include String s and int num parameters Return type is void (don’t need to return anything) We know number of times (integer), so write a for loop that runs that number of times In the for loop, print out s