Main Page

Previous Page
Next Page

[Page 126 (continued)]

4.3. Pseudocode

Pseudocode (or "fake" code) is an artificial and informal language that helps programmers develop algorithms without having to worry about the strict details of C++ language syntax. The pseudocode we present here is particularly useful for developing algorithms that will be converted to structured portions of C++ programs. Pseudocode is similar to everyday English; it is convenient and user friendly, although it is not an actual computer programming language.

Pseudocode does not execute on computers. Rather, it helps the programmer "think out" a program before attempting to write it in a programming language, such as C++. This chapter provides several examples of how to use pseudocode to develop C++ programs.

The style of pseudocode we present consists purely of characters, so programmers can type pseudocode conveniently, using any editor program. The computer can produce a freshly printed copy of a pseudocode program on demand. A carefully prepared pseudocode program can easily be converted to a corresponding C++ program. In many cases, this simply requires replacing pseudocode statements with C++ equivalents.

Pseudocode normally describes only executable statements, which cause specific actions to occur after a programmer converts a program from pseudocode to C++ and the program is run on a computer. Declarations (that do not have initializers or do not involve constructor calls) are not executable statements. For example, the declaration

        int i;

tells the compiler variable i's type and instructs the compiler to reserve space in memory for the variable. This declaration does not cause any actionsuch as input, output or a calculationto occur when the program executes. We typically do not include variable declarations in our pseudocode. However, some programmers choose to list variables and mention their purposes at the beginning of pseudocode programs.

We now look at an example of pseudocode that may be written to help a programmer create the addition program of Fig. 2.5. This pseudocode (Fig. 4.1) corresponds to the algorithm that inputs two integers from the user, adds these integers and displays their sum. Although we show the complete pseudocode listing here, we will show how to create pseudocode from a problem statement later in the chapter.

Figure 4.1. Pseudocode for the addition program of Fig. 2.5.
(This item is displayed on page 127 in the print version)

1  Prompt the user to enter the first integer
2  Input the first integer
4  Prompt the user to enter the second integer
5  Input the second integer
7  Add first integer and second integer, store result
8  Display result

Lines 12 correspond to the statements in lines 1314 of Fig. 2.5. Notice that the pseudocode statements are simply English statements that convey what task is to be performed in C++. Likewise, lines 45 correspond to the statements in lines 1617 of Fig. 2.5 and lines 78 correspond to the statements in lines 19 and 21 of Fig. 2.5.

[Page 127]

There are a few important aspects of the pseudocode in Fig. 4.1. Notice that the pseudocode corresponds to code only in function main. This occurs because pseudocode is normally used for algorithms, not complete programs. In this case, the pseudocode is used to represent the algorithm. The function in which this code is placed is not important to the algorithm itself. For the same reason, line 23 of Fig. 2.5 (the return statement) is not included in the pseudocodethis return statement is placed at the end of every main function and is not important to the algorithm. Finally, lines 911 of Fig. 2.5 are not included in the pseudocode because these variable declarations are not executable statements.

Previous Page
Next Page