Strange bug in while-loop (Eyes Only: James or TMC)

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
sotrain515
Red Slime
Posts: 59
Joined: Wed Dec 26, 2012 3:23 pm

Strange bug in while-loop (Eyes Only: James or TMC)

Post by sotrain515 »

Thought I'd mention this while I'm thinking about it. I noticed what appeared to be a bug in the scripting language while looping through some slices and it just occurred to me what might have been happening.

I had a loop that went something like this:

Code: Select all

n  := slice lookup(sli:1)
n2 := slice lookup(sli:2) #the slice immediately following sli:1
while(n, and, n2)do, begin
   set slice extra(n, extra 1, get slice extra(n2, extra 1))

   n := n2
   n2 := next sibling(n2)
end
A bug eventually started happening as I added more slices to the overall collection where the loop would drop out at about the halfway point. I added some debugging "show values" and found that the looping stopped whenever the slice values stored in n and n2 were 256 and 257, respectively (I think). I fixed the error by replacing the while loop condition with "while(n <> 0, and, n2 <> 0)do, begin". I'm guessing the error has something to do with the fact that 256 is a maximum for a signed integer and maybe 257 is -256 or something like that.... okay, maybe I don't know exactly what happened, but I think the fact that the first value was 256 has something to do with it.

Anyway, I just wanted to make note of this in case it's an unknown bug. Or maybe my initial while-condition was bunk for some reason which I'd also like to know about.

Thanks for reading!
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, your while condition is wrong. This is a common mistake. Don't use "and". Use && instead. Likewise, use || instead of "or". (No commas needed.) Best to use them by default to avoid the same problem in future.

"and" and "or" are bitwise operators. If you don't know what a bitwise operator is, there's a very simple rule: you will never need to use them.

&& and || do what you expect; they check if their left and right arguments are nonzero.

Maybe we could actually rename 'and' and 'or' to prevent this mistake; we were discussing changing -- to - in a similar breaking change.
Last edited by TMC on Wed Aug 30, 2017 6:45 pm, edited 3 times in total.
User avatar
sotrain515
Red Slime
Posts: 59
Joined: Wed Dec 26, 2012 3:23 pm

Post by sotrain515 »

Okay, thanks. Has that always been the case? I could have sworn ",and," and ",or," were the correct operators. It's so ingrained in my mind at this point it's going to take some doing to deprogram myself.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

In the olden days, ,and, and ,or, were the only options for plotscripting, so there are still tons of example scripts floating around that still use them.

,and, and ,or, are only equivalent to && and || when the values you are comparing are both really true/false

When you want to be able to assume that anything non-zero is true, you have to use && and ||

The AND and OR operators in QuickBasic/FreeBasic suffer from the same bitwise problem.

I still feel weird typing the new logical alternatives to them, ANDALSO and ORELSE
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

ANDALSO and ORELSE also feel weird to me, but I'm using them more since finding out that fbc now compiles them efficiently :)

Yes, and BASIC isn't the only other language to make this mistake. C originally also only had bitwise operators, & and |. Later it was realised logical && and || were needed, but & and | had already been given 'precedence' (their priority/way they group) as if they were logical operators, and that was kept. The result is that much of the time you use & or | in C you have to put brackets around the expression because their precedence is illogical. And many, many languages including C++, Java and Javascript inherited that problem from C.
(HamsterSpeak also has the same precedence problem)
User avatar
SwordPlay
Chemical Slime
Posts: 966
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

I did not know that about ANDALSO / ORELSE ! Never heard of it until now.
Perhaps this is why some of my scripts did not work?
Why is this not mentioned in the plotscripting dictionary?
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Virtuous Sword wrote:I did not know that about ANDALSO / ORELSE ! Never heard of it until now.
Perhaps this is why some of my scripts did not work?
Why is this not mentioned in the plotscripting dictionary?
We were talking about Freebasic

What you want is && and || which have been available in plotscripts for a long time.
User avatar
SwordPlay
Chemical Slime
Posts: 966
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

Bob the Hamster wrote:
Virtuous Sword wrote:I did not know that about ANDALSO / ORELSE ! Never heard of it until now.
Perhaps this is why some of my scripts did not work?
Why is this not mentioned in the plotscripting dictionary?
We were talking about Freebasic

What you want is && and || which have been available in plotscripts for a long time.
Ah. I never used those either.
Maybe I am just abysmal at plotscripting. And here I was getting my hopes up that it wasn't my fault :p
Last edited by SwordPlay on Sat Sep 02, 2017 12:21 am, edited 1 time in total.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

It just takes practice. Keep at it :)
User avatar
SwordPlay
Chemical Slime
Posts: 966
Joined: Sun Jan 22, 2017 9:32 am
Location: London, England
Contact:

Post by SwordPlay »

Bob the Hamster wrote:
Virtuous Sword wrote:I did not know that about ANDALSO / ORELSE ! Never heard of it until now.
Perhaps this is why some of my scripts did not work?
Why is this not mentioned in the plotscripting dictionary?
We were talking about Freebasic

What you want is && and || which have been available in plotscripts for a long time.
Ah, yeah. Thanks. And I really mean it. You guys are so helpful with any questions, it's amazing. I don't know what I would do if you guys weren't here to answer our questions sometimes. I'm sure everyone feels the same.
Post Reply