Pesky semiColons?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Pesky semiColons?

Post by dunbarx » Fri May 31, 2024 4:02 pm

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

SWEdeAndy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 270
Joined: Sat Aug 16, 2008 9:48 am
Location: Stockholm, Sweden
Contact:

Re: Pesky semiColons?

Post by SWEdeAndy » Fri May 31, 2024 5:05 pm

If you replace the semicolon with a line break, do you then see why the two examples behave differently, as they should? ;)
Andreas Bergendal
Independent app and system developer
Free LC dev tools: https://github.com/wheninspace
WhenInSpace: https://wheninspace.se/en

stam
Posts: 2776
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Pesky semiColons?

Post by stam » Fri May 31, 2024 5:48 pm

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...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Pesky semiColons?

Post by dunbarx » Fri May 31, 2024 8:36 pm

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:

Code: Select all

else doThis ; doThat
is the same as:

Code: Select all

else doThis
doThat
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.:oops:

Craig

Post Reply

Return to “Talking LiveCode”