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).

1.

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

 
 

2.

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.
 

3.

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.

 

4.

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.

 
 

5.

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.
 

6.

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.

 

7.

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.
 

8.

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

9.

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.
 

10.

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.

 
 

11.

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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net