If, else, endif problem
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
If, else, endif problem
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
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
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...
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....
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.
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
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
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
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:
but if you have so many options you may wish to use a switch statement instead - easier to manage multiple options
'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
Re: If, else, endif problem
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.)
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
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:
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
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 118 times
Last edited by Thierry on Tue Feb 23, 2021 10:52 am, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: If, else, endif 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
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
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
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
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
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
Sorry, did not look at the content of the script.
Yes, in this case a repeat loop will be more than appropriate!
Yes, in this case a repeat loop will be more than appropriate!
Re: If, else, endif problem
Very well said, Monsieur!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
Kind regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
- VIP Livecode Opensource Backer
- Posts: 7215
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: If, else, endif problem
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
HyperActive Software | http://www.hyperactivesw.com
Re: If, else, endif problem
That is true, I tend to drop the else only when I want to make sure it checks all values, my bad.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.
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: If, else, endif problem
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
http://forums.livecode.com/viewtopic.ph ... le#p130259
http://forums.livecode.com/viewtopic.ph ... ch#p156671
Craig