7.5. Pseudocode

Pseudocode is essentially generic code. When we want to specify a particular algorithm to humans, but don’t want to get bogged down in the details of a particular programming language, we write pseudocode. Pseudocode is made up and often slightly different from person to person - there is no “official” pseudocode - but the idea is always the same: express the program with commands that could easily be turned into working code in any appropriate language via simple translation. As much as possible, each line of pseudocode should correspond to on line of what would exist in real code.

Thus, something like:

Get the users test and work grades. Tell the user they passed if their overall average is 60% or above.

Would be bad pseudocode - it does not clearly explain what it means to get the average. Turning that into working code would require some problem solving, not just translation. Even the parts that do seem clear are not broken up into the kinds of statements a program uses.

The example shown below would be much more appropriate. The first tab shows pseudocode, the other two show that code translated into real programming languages. Don’t worry too much about the details of the C++ or Python code, just how they closely correspond to pseudocode. There are difference between each language and pseudocode, but they are relatively minor (at least if you know the languages involved).

1   print "Enter test average:"
2   testGrade = getInputFromUser()
3   print "Enter work average:"
4   workGrade = getInputFromUser()
5   overallGrade = (testGrade + workGrade) / 2
6   if (overallGrade >= 60)
7       print "You passed"
1   print "Enter test average:"
2   testGrade = int(input("Enter test average:"))
3   print "Enter work average:"
4   workGrade = int(input("Enter work average:"))
5   overallGrade = (testGrade + workGrade) / 2.0
6   if overallGrade >= 60:
7       print "You passed"
1   cout << "Enter test average:" << endl;
2   int testGrade;
    cin >> testGrade;
3   cout << "Enter work average:" << endl;
4   int workGrade;
    cin >> workGrade;
5   double overallGrade = (testGrade + workGrade) / 2.0;
6   if (overallGrade >= 60) {
7       cout << "You passed" << endl;

As you read on in this chapter, instead of worrying about any particular real language, we will use pseudocode to express ideas in code.