PDA

View Full Version : For loops



tanffn
03-01-2007, 04:35 PM
For I:= 0 to –1 do beep();
How many time beep() will be preformed?

Paizo
03-01-2007, 04:38 PM
:lol:
what kind of test is that? :lol:

FNX
03-01-2007, 04:52 PM
I really don't understand if this would be a trick or something... :? :lol:

JSoftware
03-01-2007, 08:06 PM
In the former version of my script language the answer would have been 1 :P

Robert Kosek
03-01-2007, 08:38 PM
0. Because once you click compile, Delphi, and even Freepascal, would beat you over the head with a very large hammer. :D

JernejL
03-01-2007, 08:56 PM
probably 0 times, unless the overflow checking is OFF, in that case it will go around and beep 4294967295 times.

Nitrogen
03-01-2007, 09:14 PM
zero times, surely!?

I routinely use this sort of code:



For I := 0 to high(GameObjects) do something;


And if high(GameObjects) returns -1, as it does when the program is first run, it just skips the for loop.

WILL
03-01-2007, 09:55 PM
Well technically if there is a thunderstorm outside and your computer is right beside the window and a bolt of lightning hits your computer case and zaps the guts of it right as you hit F9, it could possibly end up doing a single beep as it powers down it's newly fried circuits. :P

tanffn
04-01-2007, 08:00 AM
The reason I asked this is because I had a similar code to that (only difference was that -1 was a var) that compiled and ran as 90% expected (if the var was –1 it didn’t enter the loop) using Delphi6.
Few weeks ago I needed to add a few features to that application and I decided to do it with Delphi2006. The new futures I added worked fine but accompanied with it was a strange new bug..

Long story short, after several hours of debugging I found the problem with the for loop. It wasthe exact same code that worked fine in Delphi6, but in Delphi2006 I had to add an If statement before the loop!

It’s the first time I encountered this kind of behavior with for loops.. but hey, I’ve seen worse :?

WILL
04-01-2007, 03:27 PM
The most common case I've seen for needing an if statement before a for loop was when you are accessing object classes within that loop that may not have it's first element created so you'll get a memory error because the class was not pointing to memory that was allocated.

Other than that I don't see much reason to use an if statement as I've come to understand the language. Maybe avoid triggering an over anxious debugger? :)

Nitrogen
04-01-2007, 07:50 PM
I've found that there are a lot of bugs in my SP0 version of Delphi2006..

Things like the intellisense gadget doesnt know about functions I'm using from another unit that's properly declared. It refuses to see the declared unit,
and what made me think of this thread, sometimes the program would fail to compile using the normal F9, but when I did a full rebuild it worked 100%.

tanffn
07-01-2007, 09:10 AM
I wrote this test code, its the same style of the problematic code but this loop works as expected:
function getEVal(var r: integer): boolean;
begin
r:= random(2);
result:= True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
I, E: Integer;
begin
getEVal(E);
caption:= inttostr(E-1);
for I := 0 to E-1 do
caption:= caption + ' beep';
end;

This is the problematic code: (you can see the IF fixup)
SI_GetNumDevices(DeviceCount);
// look for valid device
if DeviceCount > 0 then
for I:= 0 to DeviceCount-1 do

tanffn
07-01-2007, 09:32 AM
:shock: OMG I'm soo stupid.. its soo humiliating to write this post...

After the last post I rechecked the "problematic" code, and "I" was defined there as cardinal!!! :oops: I normally use integers but in this case I used cardinal as that what was used in the imported dll. :?

Sorry guys...

Paizo
08-01-2007, 10:21 AM
:lol: no problem!
sometimes happen also to me something like a stupid error that block you for days...and you are near to destroy your pc after find the bug :D