Page 1 of 2
If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 9:35 am
by Simon Knight
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?
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 9:42 am
by FourthWorld
A single statement can follow an "else" on the same line without requiring a closing "end if".
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 10:06 am
by Simon Knight
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.
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 11:18 am
by bogs
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
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 11:48 am
by richmond62
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.

Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 12:19 pm
by LCMark
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:
or
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).
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 1:47 pm
by Simon Knight
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?
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 4:20 pm
by dunbarx
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
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 5:20 pm
by FourthWorld
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
Re: If statement without end if - why does this work?
Posted: Mon Feb 24, 2020 7:18 pm
by PaulDaMacMan
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)
Re: If statement without end if - why does this work?
Posted: Tue Feb 25, 2020 4:22 am
by mwieder
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.
Re: If statement without end if - why does this work?
Posted: Tue Feb 25, 2020 10:51 am
by bogs
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.
Re: If statement without end if - why does this work?
Posted: Tue Feb 25, 2020 2:47 pm
by dunbarx
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
Re: If statement without end if - why does this work?
Posted: Tue Feb 25, 2020 9:18 pm
by bogs
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...

- Back in the day when I was young...
Re: If statement without end if - why does this work?
Posted: Tue Feb 25, 2020 9:46 pm
by dunbarx
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