Bernard 18 Posted February 24, 2020 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
Der schöne Günther 316 Posted February 24, 2020 If you're actively exiting the loop, then everything is fine. You're probably talking about compiler warning W1037. Share this post Link to post
Rollo62 538 Posted February 24, 2020 (edited) 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 February 25, 2020 by Rollo62 Share this post Link to post
Attila Kovacs 629 Posted February 24, 2020 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
David Heffernan 2347 Posted February 24, 2020 (edited) 56 minutes ago, Bernard said: Should ToolInfo not be used outside the loop? No Edited February 24, 2020 by David Heffernan Share this post Link to post
Bernard 18 Posted February 24, 2020 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
Fr0sT.Brutal 900 Posted February 25, 2020 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
Der schöne Günther 316 Posted February 25, 2020 Then he probably has initialised the variable with nil or something else before entering the loop. Because otherwise, you get a compiler warning. Share this post Link to post
Fr0sT.Brutal 900 Posted February 25, 2020 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
Attila Kovacs 629 Posted February 25, 2020 (edited) 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 February 25, 2020 by Attila Kovacs Share this post Link to post
Fr0sT.Brutal 900 Posted February 25, 2020 (edited) 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 February 25, 2020 by Fr0sT.Brutal :) 1 Share this post Link to post