Jump to content
Sign in to follow this  
Bernard

Accessing variables outside a For loop

Recommended Posts

Hello Everyone,

 

I have a quick question about the correct way to use loop variables,  this has been answered before for the first part of the question. 

It is something that I did not find a definitive answer for in the second part of the question.

 

This is seen as bad code and I understand why

 

   For iFound := 0 to pred(Toolbox.Count) do

     if Toolbox.ID = ToolProfile.ToolName then
        break;

   iFound should not be used outside the loop to reference the found item

 

Is that the same for the following?

 

    // Find the tool profile in the part tooling list
    for ToolInfo in Toolbox do
      if ToolInfo.ID = ToolProfile.ToolName then
        break;

 

Should ToolInfo not be used outside the loop?  I am assuming the answer is no it should not.

 

regards,

 

Bernard

 

Share this post


Link to post

I would not use it that way, since the new inline variable feature may mess up things too, even if this may work in some cases.

Better be safe than sorry, and use a separate variable to monitor the status.

Edited by Rollo62

Share this post


Link to post

There is no guarantee that in the code generated by the compiler there will be any "variable" used.

Even if it works today, it could change in the future.

 

Share this post


Link to post
56 minutes ago, Bernard said:

Should ToolInfo not be used outside the loop? 

No

Edited by David Heffernan

Share this post


Link to post

Thanks for quick the replys,

 

Looks like everyone is more or less on the same page with this one.

 

That is good to know

 

Share this post


Link to post
Quote

   // Find the tool profile in the part tooling list
    for ToolInfo in Toolbox do
      if ToolInfo.ID = ToolProfile.ToolName then
        break;

Bad idea. What if "if" is never triggered?

Share this post


Link to post
2 hours ago, Der schöne Günther said:

Then he probably has initialised the variable with nil or something else before entering the loop. Because otherwise, you get a compiler warning.

Ok, what will happen after the loop? ToolInfo  will have has value of a last item in the list OR a value it was init-ed with (if list is empty) OR some value found in the list. So how the code below the loop should distinguish those cases one from another?

Share this post


Link to post
57 minutes ago, Fr0sT.Brutal said:

what will happen after the loop

After the loop != after the loop.

in the very next line a register/stack could hold the right value, a couple of lines lower it will surely hold only garbage

 

where the emphasis is on the "could".  it depends on the codegen, different compiler options, compiler versions, platforms may generate different loop code

Edited by Attila Kovacs

Share this post


Link to post
3 hours ago, Attila Kovacs said:

in the very next line a register/stack could hold the right value, a couple of lines lower it will surely hold only garbage

I was trying to express that final value will be uncertain, probably I did it unclear. Nevertheless if we're talking about for-in loops, I think nothing dangerous would happen. It's just a syntax sugar for while iterator.GetNext(variable) do ... so the value after the loop will be initial (if list is empty) or equal to last item in list. AFAIK that warning is actual for simple for loops only as compiler can optimize the counter with CPU register. This is not the case with for-in loops of course.

 

3 hours ago, Attila Kovacs said:

After the loop != after the loop.

You use inequality sign in an enemy language's style ! I suspect you're a C++ spy here in the heart of Delphiland xDDD

Edited by Fr0sT.Brutal
:)
  • Haha 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×