Difference between an fpc for loop and a delphi for loop lazarus / 61-Beginners ---------------------------------------------------------------------- EganSolo · 2015-12-14 07:36 I'm porting code from Delphi over to fpc. In delphi, the following for loop will not be executed when Count = 0. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i := 0 to count-1 do  //some code.  In Lazarus / fpc, the loop gets executed and I've got a fireworks of exception being fired because I've got a bunch of these loops in my code. I checked the compiler options, and perhaps I missed something, but I couldn't find a switch to make fpc behave like Delphi here? Please advise, before I go and add a bunch of conditional statements to my code  Thanks, Egan. ---------------------------------------------------------------------- Martin_fr · 2015-12-14 07:43 In lazarus/fpc there are many loops like that, and the do not get executed. however, check that "count" is a SIGNED integer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for i = 0 to integer(0) - 1 do  // does not loop   for i = 0 to cardinal(0) - 1 do   // I am on sure but this probably goes to a very big value. The 2nd should give an overflow error, if you turn the checks on. EDIT: It may also be that the problem is caused bi "i" being unsigned. (instead of count) ---------------------------------------------------------------------- Eugene Loza · 2015-12-14 10:31 QuoteIn Lazarus / fpc, the loop gets executed and I've got a fireworks of exception being firedFirst of all, I always do a manual range-check before loops, mostly for reasons of controlling the code and not getting unexpected behavior. A good habit, because unexecuted loop might leave some variables not initialized. Second. Well... this code always works fine for me. If "for i:=0 to -1 do" then the code is not executed. Try using {$R+}{$Q+}. And yes, as noted above, check if count is not unsigned, then the compiler might do all the operation unsigned and end up in semi- infinite loop definitely out of arrays range. ---------------------------------------------------------------------- EganSolo · 2015-12-14 18:17 You guys are write  fCount is actually a cardinal, not an integer. Delphi's behavior is consistent for cardinal and integer values, but it seems that fpc tends to be more strict with Cardinals. Good thing to keep in mind. Yes, I agree, doing a range check beforehand is good practice; Delphi was doing it for me  but I will be adding the requisite code as I god. Thanks guys, Egan. ---------------------------------------------------------------------- Martin_fr · 2015-12-14 18:37 you can activate range checks in the project/compiler options ---------------------------------------------------------------------- Leledumbo · 2015-12-15 06:07 The best practice is: Develop with all checks and debug info on, release with all of them disabled plus full optimization and strip any debug info ---------------------------------------------------------------------- Graeme · 2015-12-16 08:46 Quote from: EganSolo on December 14, 2015, 07:17:10 pmDelphi's behavior is consistent for cardinal and integer values, but it seems that fpc tends to be more strict with Cardinals. Good thing to keep in mind. Normally {$mode objfpc} is the more strict compiler mode. This is most FPC developer's prefered compiler mode too. The {$mode delphi} is the more relaxed and Delphi compatible behaviour.