Page 1 of 2
If, else, endif problem
Posted: Mon Feb 22, 2021 9:49 pm
by trags3
Hi,
I have 16 lines on a form that get filled in from checkboxes on a card from a substack.
when I open the card that has the items to be selected I make sure that the program knows the first empty line on the form I want to add to.
The checkbox selections are made and then there is a button that when pressed finds the next empty line and adds all the labels of the checkboxes that have been selected.
There are 16 fields that i check.
The fields are named 1,2,3,....thru 16.
Everything was workin fairly well, almost getting it to work when all of a sudden I get an error when checking to find the first empty line on the third empty line.
Attached is a screenshot of the code and the error message.I have been struggling with this for hours now.
LC 9.6.1 Macbook Air OS 10.15.4
Re: If, else, endif problem
Posted: Mon Feb 22, 2021 10:21 pm
by bogs
Not to sound too critical,
but (you just knew that was coming, right?

) I would like to make a couple or so suggestions.
1. this looks almost like a made to order setup for "switch / case "
2. IF (comical, eh?

) you insist on using that many 'if' statements (not like I haven't), I'd suggest you do one of two things, either ditch the else part since you are only 'then'ing one line...
Code: Select all
if field 1 of card 2 is not empty then put "NE" into field 5 of this card
if field 5 of card 2 is not empty then put "NE" into field 5 of this card
...etc
IF (hee hee) you insist on using the else, put the next if statement on the same line as the else until there is no other choices....
Code: Select all
if field 1 of card 2 is not empty then
put "NE" into field 5 of this card
else if field 5 of card 2 is not empty then
put "NE" into field 5 of this card
else
...etc
end if
Just for readability, it would be worth it, but I would ditch the else altogether myself, in this situation. If you don't want to use switch / case because you are worried it will only test one thing, just leave the 'break' out and it will tumble through all of them. However, considering you have the exit in these, I suspect that switch case would be made to order for this.
Code: Select all
switch
case field 1 of card 2 is not empty
put "NE" into field 5 of this card
break
case field 5 of card 2 is not empty
put "NE" into field 5 of this card
break
case
...etc
break
end switch
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 12:46 am
by stam
What bogs said!
'
else' is the final option within an if/end if statement.
If you have multiple alternatives you must youse 'else if' -- ie on the same line.
eg:
Code: Select all
If statement1 then
...
else if statement2 then
...
else if statement3 then
...
else
default action
end if
but if you have so many options you may wish to use a
switch statement instead - easier to manage multiple options
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 8:31 am
by SparkOut
What's been said above.
But I also note you have quoted the field and card numbers. This suggests that you have actually *named* them with a number. Really, don't do that. (I can't tell, but it might be the actual cause of your original problem.)
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 9:44 am
by Thierry
Hi,
What's been said above.
Actually, I'm a bit puzzled with your multiple else statement in your if ... end if.
the 2nd else should be a dead code; means we never execute this part.
The 'else if ...' syntax is the way to go.
I think the error you have is because of these extra else statement in lines 10,15, 20, 25
of your code.
Now thinking a bit out of the box,
here is another approach:
Code: Select all
put 0 into fld "IFA"
repeat with N=1 to the number of fields in group "gFields"
if fld N of group "gFields" is empty then
put N into field "IFA"
exit repeat
end if
end repeat
As this code works with object layers, I've put all the fields
in a group, so they are a bit more protected from wrong user's manipulation.
and you'll find below a stack I've made for you....
HTH,
Thierry
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 9:50 am
by bogs
SparkOut wrote: ↑Tue Feb 23, 2021 8:31 am
But I also note you have quoted the field and card numbers. This suggests that you have actually *named* them with a number. Really, don't do that. (I can't tell, but it might be the actual cause of your original problem.)
OH, yah, I completely forgot that!

{Thanks, SparkOut!@}
I did have another suggestion, and that is, if your going to post a code snippet, especially one that long, stick it in a code block
Just click on the little code button up there....

and then paste the code between the two "[ code ]" tags. Aside from being neater in a thread, it allows us to copy / paste it if we wanted to use exactly your code.
Naming things is much like what SparkOut said, because Lc shows objects in a number of ways, but numbers are used by Lc for layer and count positions.
If you have a card named "myCard" <-- quote around the actual name
if your looking for card 1 <-- no quote around the number
or if your looking for button 4 <-- fourth button on the card, also happens to be the layer it resides on, then no quotes.
Lastly, Thierry has chimed in with another way to clean up what your actually trying to accomplish, by looping through the controls, which makes even more sense than if / then or switch / case for this kind of problem. Good call Thierry

Re: If, else, endif problem
Posted: Tue Feb 23, 2021 10:43 am
by stam
Lots of helpful info here for you @trags3, but a lot to sift through...
Take away messages:
-
NEVER use a number as a field name -- call it 'f1', or 'field 1' or 'spaceship1' instead of '1', or you
WILL get unexpected results
-
You can only use 1 'else' statement, for multiple options use 'else if'
- if you have a large number of alternatives, use a
switch statement instead
- when you see code that is repeated identically except for a change in variable names, refactor your code! Thierry illustrate a nice loop that does the same with many fewer line of code.
- Make it easier for people to help you -- copy paste your code as text in a code block, instead of pasting a pic

Re: If, else, endif problem
Posted: Tue Feb 23, 2021 10:54 am
by bogs
stam wrote: ↑Tue Feb 23, 2021 10:43 am
- You can only use 1 'else' statement, for multiple options use 'else if'....
Or, if they are only one liners after the 'then', just continue them on the same line
if a then b
if c then d
etc.
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 11:45 am
by Klaus
I would also use SWITCH CASE in this case!
I never understood when and where an END IF is neccessary with all these nested IF THEN ELSEIF.
Besides the fact that a SWITCH statement is definitively better readable and understandable with many conditions.
AND:
viewtopic.php?f=7&t=35424&p=202023&hili ... ER#p202013

Re: If, else, endif problem
Posted: Tue Feb 23, 2021 12:24 pm
by bogs
Over the loop Klaus? I dunno, I think I'd loop it, for one thing, a HECK of a lot less typing = less chance you goof. Read-ability trumps either 'if / then' or 'switch / case'.
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 12:36 pm
by Klaus
Sorry, did not look at the content of the script.
Yes, in this case a repeat loop will be more than appropriate!
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 2:14 pm
by Thierry
stam wrote: ↑Tue Feb 23, 2021 10:43 am
Take away messages:
-
NEVER use a number as a field name
call it 'f1', or 'field 1' or 'spaceship1' instead of '1', or you
WILL get unexpected results
-
You can only use 1 'else' statement, for multiple options use 'else if'
- if you have a large number of alternatives, use a
switch statement instead
- when you see code that is repeated identically except for a change in variable names,
refactor your code!
- Make it easier for people to help you
copy paste your code as text in a code block, instead of pasting a pic
Very well said, Monsieur!
Kind regards,
Thierry
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 6:38 pm
by jacque
Thierry wrote: ↑Tue Feb 23, 2021 9:44 am
Actually, I'm a bit puzzled with your multiple else statement in your if ... end if.
the 2nd else should be a dead code; means we never execute this part.
Actually, it's valid and runs because the compiler combines the lines correctly. I used to see it a lot when converting old HC stacks for clients.
I wouldn't use Bogs' suggestion to eliminate all the "else" statements because then every "if" will run even when the first one matches. The else statements perform similarly to a switch structure and exit after the first match.
But for readability and the ability to combine or fall through, I prefer switch structures. My informal, generic rule is that if there are more than two or three conditions I use a switch.
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 6:44 pm
by bogs
I wouldn't use Bogs' suggestion to eliminate all the "else" statements because then every "if" will run even when the first one matches. The else statements perform similarly to a switch structure and exit after the first match.
That is true, I tend to drop the else only when I want to make sure it checks all values, my bad.
Re: If, else, endif problem
Posted: Tue Feb 23, 2021 7:50 pm
by dunbarx