Main Page

Previous Page
Next Page

[Page 1374 (continued)]

L.4. Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands

Sometimes you will need to execute a program line by line to find and fix logic errors. Walking through a portion of your program this way can help you verify that a function's code executes correctly. In this section, you will learn how to use the debugger for this task. The commands you learn in this section allow you to execute a function line by line, execute all the statements of a function at once or execute only the remaining statements of a function (if you have already executed some statements within the function).

Setting a breakpoint. Set a breakpoint at line 25 by clicking in the margin indicator bar (Fig. L.21).

Figure L.21. Setting a breakpoint in the program

[Page 1375]
Starting the debugger. Select Debug > Start. Enter the value 13 at the Enter withdrawal amount for account1: prompt. Execution will halt when the program reaches the breakpoint at line 25.

Using the Step Into command. The Step Into command executes the next statement in the program (the yellow highlighted line of Fig. L.24) and immediately halts. If the statement to be executed as a result of the Step Into command is a function call, control is transferred to the called function. The Step Into command allows you to enter a function and confirm its execution by individually executing each statement inside the function. Select Debug > Step Into to enter the debit function (Fig. L.22). If the debugger is not at line 33, select Debug > Step Into again to reach that line.

Figure L.22. Stepping into the debit function.

Using the Step Over command. Select Debug > Step Over to execute the current statement (line 33 in Fig. L.22) and transfer control to line 34 (Fig. L.23). The Step Over command behaves like the Step Into command when the next statement to execute does not contain a function call. You will see how the Step Over command differs from the Step Into command in Step 10.

Figure L.23. Stepping over a statement in the debit function.

Figure L.24. Using the Step Into command to execute a statement.

[Page 1376]
Using the Step Out command. Select Debug > Step Out to execute the remaining statements in the function and return control to the next executable statement (line 28 in Fig. L.3), which contains the function call. Often, in lengthy functions, you will want to look at a few key lines of code, then continue debugging the caller's code. The Step Out command is useful for such situations, where you do not want to continue stepping through the entire function line by line.

Setting a breakpoint. Set a breakpoint (Fig. L.25) at the return statement of main at line 29 of Fig. L.3. You will make use of this breakpoint in the next step.

Figure L.25. Setting a second breakpoint in the program.

Using the Continue command. Select Debug > Continue to exectue until the next breakpoint is reached at line 29. This feature saves time when you do not want to step line by line through many lines of code to reach the next breakpoint.

Stopping the debugger. Select Debug > Stop Debugging to end the debugging session. This will close the Command Prompt window.

Starting the debugger. Before we can demonstrate the next debugger feature, you must start the debugger again. Start it, as you did in Step 2, and enter as input the same value (13). The debugger pauses execution at line 25.

Using the Step Over command. Select Debug > Step Over (Fig. L.26) Recall that this command behaves like the Step Into command when the next statement to execute does not contain a function call. If the next statement to execute contains a function call, the called function executes in its entirety (without pausing execution at any statement inside the function), and the yellow arrow advances to the next executable line (after the function call) in the current function. In this case, the debugger executes line 25, located in main (Fig. L.3). Line 25 calls the debit function. The debugger then pauses execution at line 28, the next executable line in the current function, main.

Figure L.26. Using the debugger's Step Over command.

[Page 1377]
Stopping the debugger. Select Debug > Stop Debugging. This will close the Command Prompt window. Remove all remaining breakpoints.

In this section, you learned how to use the debugger's Step Into command to debug functions called during your program's execution. You saw how the Step Over command can be used to step over a function call. You used the Step Out command to continue execution until the end of the current function. You also learned that the Continue command continues execution until another breakpoint is found or the program exits.

Previous Page
Next Page