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: 10013
Joined: Wed May 06, 2009 2:28 pm

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: 296
Joined: Sat Aug 16, 2008 9:48 am
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
(WIS_WebDeployHelper, WIS_ScriptDependencies, WIS_BrowserAnimation)
WhenInSpace: https://wheninspace.com

stam
Posts: 2995
Joined: Sun Jun 04, 2006 9:39 pm

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: 10013
Joined: Wed May 06, 2009 2:28 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9755
Joined: Fri Feb 19, 2010 10:17 am

Re: Pesky semiColons?

Post by richmond62 » Thu Sep 12, 2024 8:30 am

Deleted rather redundant post.
Last edited by richmond62 on Thu Sep 12, 2024 8:41 am, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9755
Joined: Fri Feb 19, 2010 10:17 am

Re: Pesky semiColons?

Post by richmond62 » Thu Sep 12, 2024 8:41 am

I have just made a silly stack:
-
Screenshot 2024-09-12 at 10.37.17.png
-
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. 8)

So: it can only be leaving an IF statement without an END IF that screws things up.
Attachments
Semi-Colon.livecode.zip
Stack.
(863 Bytes) Downloaded 129 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10013
Joined: Wed May 06, 2009 2:28 pm

Re: Pesky semiColons?

Post by dunbarx » Thu Sep 12, 2024 2:11 pm

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

Post Reply

Return to “Talking LiveCode”