M.3. The print and set Commands
In the preceding section, you learned how to use the debugger's print command to examine the value of a variable during program execution. In this section, you will learn how to use the print command to examine the value of more complex expressions. You will also learn the set command, which allows the programmer to assign new values to variables. We assume you are working in the directory containing this appendix's examples and have compiled for debugging with the -g compiler option.
Evaluating arithmetic and boolean expressions. Recall from Section M.2 that once the program has entered break mode, you can explore the values of the program's variables using the debugger's print command. You can also use the print command to evaluate arithmetic and boolean expressions. Type print withdrawalAmount - 2. Note that the print command returns the value 11 (Fig. M.17). However, this command does not actually change the value of withdrawalAmount. Type print withdrawalAmount == 11. Expressions containing the == symbol are treated as boolean expressions. The value returned is false (Fig. M.17) because withdrawalAmount does not currently contain the value 11withdrawalAmount is still 13.
Figure M.17. Printing expressions with the debugger.
Modifying values. The debugger allows you to change the values of variables during the program's execution. This can be valuable for experimenting with different values and for locating logic errors in programs. You can use the debugger's set command to change the value of a variable. Type set withdrawalAmount = 42. The debugger changes the value of withdrawalAmount. Type print withdrawalAmount to display its new value (Fig. M.18).
Figure M.18. Setting the value of a variable while in break mode.
Viewing the program result. Type continue to continue program execution. Line 25 of Fig. M.3 executes, passing withdrawalAmount to Account member function debit. Function main then displays the new balance. Note that the result is $8 (Fig. M.19). This shows that the preceding step changed the value of withdrawalAmount from its initial value (13) to 42.
Figure M.19. Using a modified variable in the execution of a program.
In this section, you learned how to use the debugger's print command to evaluate arithmetic and boolean expressions. You also learned how to use the set command to modify the value of a variable during your program's execution.