Wandering Soulstar Posted May 24, 2019 Share Posted May 24, 2019 Hi All, Had a snippet of code that was driving me mad .. got it fixed but I do not understand what was going on ... here is what I had (redacted for clarity) integer num = 8; integer found = FALSE; while (num -= 2 >= 0 && !found) { //do something to check found } What was hapening was that with the code was running through the loop SEVEN times, as opposed to the four that I would expect, going from 7 to 1. I got it to work by enclosing the 'num -= 2' in parenthesis. FIne, undertand about forcing the calculation prior to the conditional check or whatever ... what I do not get though is the behaviour seen without the parenthesis. Why would it do a -1 of num? Link to comment Share on other sites More sharing options...
Wulfie Reanimator Posted May 24, 2019 Share Posted May 24, 2019 (edited) See operator precedence: http://wiki.secondlife.com/wiki/LSL_Operators The -= and >= operators share precedence, so they will be evaluated "at the same time" from right to left. // original line: while (num -= 2 >= 0 && !found) {...} // what happens: while ((num -= (2 >= 0)) && !found) {...} // (2 >= 0) becomes TRUE aka 1 while ((num -= (1)) && !found) {...} Edited May 24, 2019 by Wulfie Reanimator 1 1 Link to comment Share on other sites More sharing options...
Profaitchikenz Haiku Posted May 24, 2019 Share Posted May 24, 2019 Dam, pipped at the post! A similar gotcha can occur when one of your comparisons contains a function call, it is crucial to satisfy yourself that you know the sequence in which all the values to be tested will occur. I would always recommend parenthesising any complex test anyway, but that's just me, I'm old, I can't keep as much in my short-term memory as I used to, and I've made exactly this sort of mistake so many times in the past... 1 Link to comment Share on other sites More sharing options...
Wandering Soulstar Posted May 24, 2019 Author Share Posted May 24, 2019 AHHH <face palm> ... of course should have thought of that .... coding 101 .. thanks so much @Wulfie Reanimator Link to comment Share on other sites More sharing options...
Kyrah Abattoir Posted May 25, 2019 Share Posted May 25, 2019 Precedence is funny in LSL sometimes, parenthesis don't cost anything memory/speed wise, so use them anytime you have doubts. Link to comment Share on other sites More sharing options...
Wulfie Reanimator Posted May 25, 2019 Share Posted May 25, 2019 2 hours ago, Kyrah Abattoir said: Precedence is funny in LSL sometimes, parenthesis don't cost anything memory/speed wise, so use them anytime you have doubts. LSL's operator precedence is pretty in-line with other languages, in fact it's almost identical with C# (my other language). Link to comment Share on other sites More sharing options...
Kyrah Abattoir Posted May 25, 2019 Share Posted May 25, 2019 (edited) 7 hours ago, Wulfie Reanimator said: LSL's operator precedence is pretty in-line with other languages, in fact it's almost identical with C# (my other language). Yeah but from memory there is a weird quirks related to && || operations that has me put parenthesis just to be sure. Edited May 25, 2019 by Kyrah Abattoir Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now