Page 1 of 1
Pesky semiColons?
Posted: Fri May 31, 2024 4:02 pm
by dunbarx
Should have put this in the beginners section. Am I the only one on the planet who just now discovered that, given:
Code: Select all
if this then
XYZ
else doThis ; doThat
is NOT the same as:
Code: Select all
if this then
XYZ
else
doThis
doThat
end if
In the first snippet, only "do this" is bound by the "else" clause. "doThat" will execute no matter what, the same as if one placed that it on its very own line below the entirety of the if/then construct.
The semiColon seems to be a convenience for
looking at lines of code, but it actually is two
very independent lines. If such a line follows "else", it will not be part of that "else".
Craig
Re: Pesky semiColons?
Posted: Fri May 31, 2024 5:05 pm
by SWEdeAndy
If you replace the semicolon with a line break, do you then see why the two examples behave differently, as they should?

Re: Pesky semiColons?
Posted: Fri May 31, 2024 5:48 pm
by stam
dunbarx wrote: ↑Fri May 31, 2024 4:02 pm
Code: Select all
if this then
XYZ
else doThis ; doThat
is NOT the same as:
Code: Select all
if this then
XYZ
else
doThis
doThat
end if
I can't test right now, but I'm not sure
Code: Select all
if this then
XYZ
else
doThis; doThat
end if
is different from your second example.
Your first example doesn't have an
end if, and LC permits the form:
Code: Select all
if <condition> then <action>
else <another action>
But any subsequent lines in that form are no longer part of the conditional. For me this is dangerous syntax as while convenient it can lead to errors.
I always err on the side of putting and
end if tag at the end of my conditionals, and avoid the latter syntax to avoid errors...
Theoretically though, if I understood correct how ; works, this would be valid:
Code: Select all
if this then
XYZ
else ; doThis ; doThat ; end if
This should replicate your second conditional...
Re: Pesky semiColons?
Posted: Fri May 31, 2024 8:36 pm
by dunbarx
Stam, SwedeAndy.
Thanks for the feedback.
I am not really astonished at anything apart from not having this bitten me earlier. After all, if a semiColon is just shorthand for a line break, then this:
is the same as:
And now it is obvious that the "doThis" is indeed outside of the if/then construction. Makes perfect sense. I just had a bit of trouble debugging my code, where that last clause fired when I assumed it should not. I complained here too hastily.
Craig
Re: Pesky semiColons?
Posted: Thu Sep 12, 2024 8:30 am
by richmond62
Deleted rather redundant post.
Re: Pesky semiColons?
Posted: Thu Sep 12, 2024 8:41 am
by richmond62
I have just made a silly stack:
-
-
And the button "Button" contains this code:
Code: Select all
on mouseUp
if the backGroundColor of btn "Button" is white then
set the backGroundColor of btn "Button" to yellow; set the textColor of btn "Button" to red
else
set the backGroundColor of btn "Button" to white; set the textColor of btn "Button" to black
end if
end mouseUp
And it changes BOTH values every time.
So: it can only be leaving an IF statement without an END IF that screws things up.
Re: Pesky semiColons?
Posted: Thu Sep 12, 2024 2:11 pm
by dunbarx
Richmond.
Well, correct, a semiColon is really just a convenience for "assembling and reading" code. I use it to keep similar or "linked" portions of my code in a more readable way
My original (incorrect) assumption was that a line of code that contained two or more semicolon-separated "clauses" were somehow locked into a single line of the "else" portion of an if-then construction. That is incorrect, and, looking back on it, wrongheaded.
Craig