If, else, endif problem

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

trags3
Posts: 363
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

If, else, endif problem

Post by trags3 » Mon Feb 22, 2021 9:49 pm

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
Attachments
Screen Shot 2021-02-22 at 12.28.30 PM.png

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

Re: If, else, endif problem

Post by bogs » Mon Feb 22, 2021 10:21 pm

Not to sound too critical, but (you just knew that was coming, right? :twisted: ) 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? :D ) 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
Image

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

Re: If, else, endif problem

Post by stam » Tue Feb 23, 2021 12:46 am

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

SparkOut
Posts: 2407
Joined: Sun Sep 23, 2007 4:58 pm

Re: If, else, endif problem

Post by SparkOut » Tue Feb 23, 2021 8:31 am

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 817
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: If, else, endif problem

Post by Thierry » Tue Feb 23, 2021 9:44 am

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
Attachments
test4trags3.livecode.zip
(1.31 KiB) Downloaded 4 times
Last edited by Thierry on Tue Feb 23, 2021 10:52 am, edited 1 time in total.
Thierry Douez
- Senior computer scientist
- Conseil, dévelopement et support en Français
https://sunny-tdz.com
https://sunny-tdz.com/livecode/externals

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

Re: If, else, endif problem

Post by bogs » Tue Feb 23, 2021 9:50 am

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! Image
{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 :D

Just click on the little code button up there....
Image
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 :D
Image

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

Re: If, else, endif problem

Post by stam » Tue Feb 23, 2021 10:43 am

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 :)

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

Re: If, else, endif problem

Post by bogs » Tue Feb 23, 2021 10:54 am

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

Klaus
Posts: 12204
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: If, else, endif problem

Post by Klaus » Tue Feb 23, 2021 11:45 am

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
:D

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

Re: If, else, endif problem

Post by bogs » Tue Feb 23, 2021 12:24 pm

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

Klaus
Posts: 12204
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: If, else, endif problem

Post by Klaus » Tue Feb 23, 2021 12:36 pm

Sorry, did not look at the content of the script.
Yes, in this case a repeat loop will be more than appropriate!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 817
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: If, else, endif problem

Post by Thierry » Tue Feb 23, 2021 2:14 pm

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
Thierry Douez
- Senior computer scientist
- Conseil, dévelopement et support en Français
https://sunny-tdz.com
https://sunny-tdz.com/livecode/externals

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5838
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: If, else, endif problem

Post by jacque » Tue Feb 23, 2021 6:38 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: If, else, endif problem

Post by bogs » Tue Feb 23, 2021 6:44 pm

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

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

Re: If, else, endif problem

Post by dunbarx » Tue Feb 23, 2021 7:50 pm

I am reminded of this "switch" thread, which I still believe would be of great value to all of us:

http://forums.livecode.com/viewtopic.ph ... le#p130259

http://forums.livecode.com/viewtopic.ph ... ch#p156671



Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”