Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12376

Can't step into for loop

I've been having a strange problem that I've never seen before. Now I have a for loop:

var
 a,counter: byte;
begin
  a:=0; 
   for counter := 1 to 10 do//I put a breakpoint at this line
   begin
     a:=a*5;
     a:=a+counter;
   end;
end;

If I put a breakpoint at the line above and try to step into the loop I can't do it. The debugger immediately steps over the loop and goes to the end.In the end I get the right result, but I can't follow the loop step by step. I mean this is just a simple example and not the real task. I just want to know when in what circumstances does this happen? I definitely remember tracking through all the steps of a loop. I work with Delphi 2010.

Upvotes: 2

Views: 1593

Answers (3)

LU RD
LU RD

Reputation: 34947

In a comment to Ken's answer, Mikayil hinted that the code is inside a procedure. This would also be a sound assumption looking at the code.

So if we set up a test like this :

Procedure Test;
var
  a,counter: byte;
begin
  a:=0;
  for counter := 1 to 10 do//I put a breakpoint at this line
  begin
    a:=a*5;
    a:=a+counter;
  end;
end;

begin
  Test;
end.

Set optimization on : Result - as observed by Mikayil, no stepping into loop possible.

Set optimization off : Result - stepping into loop possible, just as ain suggested.

Now also take into consideration, Mikayil's question in Ken's answer : whether the inability to step into the loop was because of the local scope of the a.

Ken answered no, but this is not the case :

var
  a : byte; // scope of a is outside of the procedure

Procedure Test;
var
  counter: byte;
begin
  a:=0;
  for counter := 1 to 10 do//I put a breakpoint at this line
  begin
    a:=a*5;
    a:=a+counter;
  end;
end;

begin
  Test;
end.

Now it does not matter whether optimization is on or off, stepping into the loop is possible anyway.

So, ain is absolute correct in his answer. (Tested in XE2)

Update :

For enabling stepping into the loop there are three possibilities :

  1. Set optimization off.
  2. Declare a outside your local scope.
  3. Insert a dummy operation using a after the loop. Like : if (a < counter) then;

Neither of these steps are uncommon debug procedures, which I find this question is all about.

Upvotes: 3

Ken White
Ken White

Reputation: 125767

Both lines of code in the loop can be completely optimized away; you do nothing with a outside the loop, so both of the assignments are unnecessary. After the optimization, the compiler is leaving

for counter := 1 to 10 do 
  ;

Actually, if you didn't have a breakpoint there, the loop would be removed as well, as it does nothing.

If you're having problems with your code, and the info above doesn't help (using the variable a after the loop runs), you need to post your real code. This made-up code is very clear to analyze; the problem in your actual code may be this simple, or much more complex to analyze.

Upvotes: 10

ain
ain

Reputation: 22769

See does turning off optimization makes difference - in project options -> Compiling -> Code generation.

Upvotes: 7

Related Questions