Getting Delphi to run only 1 line at a time

1.9k views Asked by At

I want to run my delphi program, but I need to see how many times a specific loop is executed. I remember in school when using Delphi 7 we used to set it so that when running it would only run 1 line at a time instead of run all of them at once. I can unfortunately not, for the life of me, remember how to do this.

How do you set Delphi to only run one line at a time and require you to step it forward before running the next one?

4

There are 4 answers

0
gabr On BEST ANSWER

F7 will execute one line (and will step into a subroutine).

F8 will execute one line (but will not step into a subroutine so actually many lines can be executed).

Shortcut keys are documented here.

0
David Heffernan On

Run the program in the debugger. Typically you would start this by pressing F9, but that will set the program running. Instead you would use the Step Over action, with shortcut F8. Then each time you want to step over a line, press F8 again. As the name implies, if the line has a function call you will "step" over that call. To step into a function use Step Into, F7. When you do that the debugger will move to the first line of the function.

If you start the program in stepping mode, it might take you a while to reach the point of interest. Get there more quickly by setting a breakpoint with the F5 key, and by the Run to Cursor action, shortcut F4.

All of these actions can be found on the menu or the toolbar, but it is crushingly slow to use the mouse to step through code. That's why all half-way proficient developers use the keyboard shortcuts when debugging. It is way more productive and that really matters when debugging.

For more information, please refer to the documentation: http://docwiki.embarcadero.com/RADStudio/en/Overview_of_Debugging

4
SilverWarior On

You say that you need to know how many times a certain loop executes.

One way to do this is by placing a breakpoint on one of the lines inside the loop that is executed each time (not inside some conditional block or another nested loop) running your application in Debug Mode (F9 key shortcut) which would cause your program to stop once it reaches that breakpoint.

Then you use F9 key to step over that breakpoint to the next one and count how many times did your program stop at that specific breakpoint.


While the above approach is easy to setup it is not very friendly if you have loop with lots of iterations (a few dozens) as it can take you quite some time to count each iteration manually. Not to mention that you can easily miscount yourself.

So in order to avoid the above mentioned problems you can ask the debugger to help you with counting. How do you do this?

You do this in two steps.

In first step you place one breakpoint inside your loop like in the first example. But this time after you place it you right click on it and chose Breakpoint Properties from the Popup Menu. This will open Breakpoint Properties Dialog from where you can define different properties which affects what happens when the breakpoint is reached.

Breakpoint properties dialog

The property that you are interested here is Pass count.

Usually this property is used to break your program after specific breakpoint is passed for certain times.

But in our case we are not interested in stopping of our program but instead in counting the number of passes so you should set its value to be higher than the expected number of loop iterations so that this breakpoint won't cause your program to stop at all.

Then the next step is to add additional breakpoint on the first line which will be executed after the loop.

So your setup should now stop your program just after the loop and you can evaluate how many times was the first breakpoint (the one inside your loop) passed by moving your mouse cursor over it.

As you will notice the number of passes is shown as Pass count: X of Y where X is the number of times that breakpoint has already been passed and Y is the amount of passes that you specified in the breakpoint properties.


This second approach makes counting of loop iteration much easier especially when you have a few dozens or even a few hundreds of loop iterations.

But bear in mind that this approach can only be used to count the number of your loop iterations the first time the loop is used. Why?

Because the debugger is counting the number of breakpoint passes since the program start. So entering a loop again later would mean that the debugger would just continue counting from where it stopped last time.


You can however force the debugger to reset the counter by setting it to 0 and leaving your program to stop on it. After that you can again set the breakpoint Pass count property to some larger number.

NOTE: Resetting the breakpoint pass counter like explained above does needed you to step into one loop iteration so the number of total loop iteration returned will be lowered by one (the loop iteration that your program stopped in in order to reset the counter).

5
Pieter B On

With all the talk about adding breakpoints and stepping through the code I want to offer another option: use a profiler.

With a profiler (like AQtime) you can easily find out how often which function or line in your program is executed without having to add additional variables or stepping through the code in a debugger.

If the only information you're interested in is the amount of times that a loop is executed, I think this is the cleanest option.