If statement without end if - why does this work?

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

If statement without end if - why does this work?

Post by Simon Knight » Mon Feb 24, 2020 9:35 am

Hi,

I am using some code that Scott Rossi published and I would like to understand why it does not throw an error :

Code: Select all

command updateMyValue
   local theRange, thePercent, theAmount, theStartValue
   local theArcValue, 
   local tGuageValue, tStartValue, tEndValue
   lock screen
   put abs(myEndValue() - myStartValue()) into theRange
   put ((myGaugeValue() - myStartValue())/theRange) into thePercent
   if myStartValue() > myEndValue() then put (1 - thePercent) into thePercent
   put round(thePercent * 360) into theAmount
   
   -- WHY IS THIS "IF" ALLOWED WITHOUT AN "END IF" ?
   if myFillDirection() is "clockwise" then
      put (450 - theAmount) into theStartValue
   else put 90 into theStartValue
   put theAmount into theArcValue
   set startAngle of myFill() to theStartValue
   set arcAngle of myFill() to theArcValue
   set label of myDisplay() to myGaugeValue()
   unlock screen
end updateMyValue
The code does not raise any errors and runs o.k.
I have checked the dictionary and confirmed that all the examples of multi line if statements end with an end if. In the example above it is not clear , to me, what gets actioned following the else statement. Have I missed something obvious?
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10060
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: If statement without end if - why does this work?

Post by FourthWorld » Mon Feb 24, 2020 9:42 am

A single statement can follow an "else" on the same line without requiring a closing "end if".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: If statement without end if - why does this work?

Post by Simon Knight » Mon Feb 24, 2020 10:06 am

Thanks Richard,

I always thought that once the statement was multi line it had to be closed with an end so you live and learn. I can't decide if the form above is an aid or hinderance to understanding code.
best wishes
Skids

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: If statement without end if - why does this work?

Post by bogs » Mon Feb 24, 2020 11:18 am

If the statement was in this form -

Code: Select all

   if myFillDirection() is "clockwise" then put (450 - theAmount) into theStartValue else put 90 into theStartValue
- then it would perhaps be clearer to you why it works.
I always thought that once the statement was multi line it had to be closed with an end so you live and learn
Yes, but those are multiple lines immediately following the 'then' part, such as -

Code: Select all

   if myFillDirection() is "clockwise" then
      put (450 - theAmount) into theStartValue
      put "this is another statement" into tVar --< At this point, an end if will be required...
   else 
      put 90 into theStartValue
   end if
Image

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

Re: If statement without end if - why does this work?

Post by richmond62 » Mon Feb 24, 2020 11:48 am

Um: all slightly inconsistent methinks.

Mind you, I'm having a few problems at the moment
trying to work out when some Python conditional loops finish. :evil:

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1234
Joined: Thu Apr 11, 2013 11:27 am

Re: If statement without end if - why does this work?

Post by LCMark » Mon Feb 24, 2020 12:19 pm

There's an old 'bug' report about the forms of 'if' here https://quality.livecode.com/show_bug.cgi?id=2694 - it lists all forms the engine recognizes to explain why the report wasn't a bug.

Due to this, I always strongly recommend only using either:

Code: Select all

if X then Y [ else Z ]
or

Code: Select all

if X then
  Y
else
  Z
end if
And never using the single-line form inside a multi-line form (else you get precisely the issue the non-bug report linked above was about).

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: If statement without end if - why does this work?

Post by Simon Knight » Mon Feb 24, 2020 1:47 pm

Being a bear of little brain I could not agree more, I'm all in favour of clear code over concise code especially if it helps avoid the "dangling else" issue.

Re reading the dictionary, well o.k. reading all the way to the end of the entry reveals this:
If one if control structure is nested inside another, use of the second form described above is recommended, since the other forms may cause ambiguities in interpreting which else clause belongs with which if statement.
With the second form listed as :

Code: Select all

if condition then
   statementList 
[else
   elseStatementList]
end if
Would it be useful to include all the examples given in the bug report in the dictionary entry, along with the recommendation to only use two of them?
best wishes
Skids

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

Re: If statement without end if - why does this work?

Post by dunbarx » Mon Feb 24, 2020 4:20 pm

I have been using all the styles of "if/then" since 1987. It was HC that allowed and delineated its many possible forms. We all likely have changed the style with changes to the code, either to increase its complexity or reduce it.

I have started to use switch constructions even for "small" conditional snippets, anything much more than a one liner. Except for the "break" lines, which may make the snippet longer than its simple "if/then" counterpart, I find them much easier to read and to manage.

The switch construction has only one "form". It would be possible, though perhaps strained, never to use "if/then" at all.

Just saying...

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10060
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: If statement without end if - why does this work?

Post by FourthWorld » Mon Feb 24, 2020 5:20 pm

Simon Knight wrote:
Mon Feb 24, 2020 1:47 pm
Would it be useful to include all the examples given in the bug report in the dictionary entry, along with the recommendation to only use two of them?
I just submitted an enhancement request for that:
https://quality.livecode.com/show_bug.cgi?id=22589

Documentation enhancements like these make great ways for community members to get started with submitting revisions to LiveCode.
https://github.com/livecode/livecode/bl ... IBUTING.md
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PaulDaMacMan
Posts: 683
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: If statement without end if - why does this work?

Post by PaulDaMacMan » Mon Feb 24, 2020 7:18 pm

Just wanted to note here that unlike LiveCode Script, LiveCode Builder does not allow single-line if/then at all (which has tripped me up several times while switching back and forth between the two)
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: If statement without end if - why does this work?

Post by mwieder » Tue Feb 25, 2020 4:22 am

While I applaud the lack of single-line conditionals in LCB, I should point out that there is also no switch statment in LCB, which I find more of a serious drawback.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: If statement without end if - why does this work?

Post by bogs » Tue Feb 25, 2020 10:51 am

I once again appear to be in the minority opinion, I like single line conditional statements and think they read just fine. I am such a schmaltz. Probably a carry over from my time in (most) Basic's and Delphi.
Image

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

Re: If statement without end if - why does this work?

Post by dunbarx » Tue Feb 25, 2020 2:47 pm

Bogs.

You are rendered chicken fat?

I think you need to brush up on your Yiddish. Check out Danny Goodman's "Complete Yiddish Handbook".

Craig

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: If statement without end if - why does this work?

Post by bogs » Tue Feb 25, 2020 9:18 pm

dunbarx wrote:
Tue Feb 25, 2020 2:47 pm
Bogs.
You are rendered chicken fat?
I think you need to brush up on your Yiddish. Check out Danny Goodman's "Complete Yiddish Handbook".
Craig
Um, no heh. Well, maybe, actually, but in this case...
aPic_schmaltz.png
Back in the day when I was young...
Image

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

Re: If statement without end if - why does this work?

Post by dunbarx » Tue Feb 25, 2020 9:46 pm

Oh, THAT schmaltz.

You meant that you were schmaltzy, not that you were a schmaltz. You can wade in a pile of schmaltz, watching, say, a vapid forgettable movie.

But you cannot BE a schmaltz. Note that you can indeed be a schlemiel.

Craig

Post Reply