more than one action for if else
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
more than one action for if else
Hi
I have tried various programming languages and if/else is a neccessity in every language.
My question is, how do I perform more than one action using if/else statements?
For example:
if <condition>
then do <this>
and <this>
and <that>
else if <condition>
then do <this>
and <this>
and <that>
Also, I keep having this "missing end if" error.
I have tried various programming languages and if/else is a neccessity in every language.
My question is, how do I perform more than one action using if/else statements?
For example:
if <condition>
then do <this>
and <this>
and <that>
else if <condition>
then do <this>
and <this>
and <that>
Also, I keep having this "missing end if" error.
-
- Livecode Opensource Backer
- Posts: 1336
- Joined: Sun Jul 12, 2009 10:53 am
- Location: Bordeaux, France
Re: more than one action for if else
Do something like...
Have a look at 'switch' in the dictionary if you have lots of choices to consider
Code: Select all
on mouseUp
put random(2) into theChoice
if theChoice = 1 then
put "England" into theCountry
put "Beer" into theAssociation
else
put "Scotland" into theCountry
put "Whisky" into theAssociation
end if
put theCountry & comma & theAssociation
end mouseUp
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: more than one action for if else
As Klaus says, first start with the dictionary, then practice at home. These control structures (if-then and switch) are fully featured and amazingly powerful and flexible.
And readable, which might be the most important feature of all.
Craig Newman
And readable, which might be the most important feature of all.
Craig Newman
Re: more than one action for if else
Oh I see, so in Livecode the indentations and paragraphing does matter,
I think I'm ok for now, thanks!
I think I'm ok for now, thanks!
Re: more than one action for if else
The formatting is done automatically by the IDE, but it has no effect on the code - if it weren't indented it would still work.
The important thing here is that an if statement is Livecode is
If *condition* then
do some stuff, could be multiple lines of code, could even have more if/thens within it
else (condition above is not met)
do other stuff. Again, could be multiple lines of code, and have other conditional statements in it
end if
The other option mentioned above is the switch statement, where you can have multiple conditions, each with their own action
switch
case condition 1
do some stuff, could be multiple lines of code
break
case condition 2
do some stuff, could be multiple lines of code
break
case condition 3
do some stuff, could be multiple lines of code
break
condition 4
do some stuff, could be multiple lines of code
break
case condition 5
do some stuff, could be multiple lines of code
default
do this stuff if none of the above conditions are met
end switch
Read up on if/then, switch and case in the dictionary for a full explanation of what they can do.
EDIT: added the case preceding the conditions in the switch statement
The important thing here is that an if statement is Livecode is
If *condition* then
do some stuff, could be multiple lines of code, could even have more if/thens within it
else (condition above is not met)
do other stuff. Again, could be multiple lines of code, and have other conditional statements in it
end if
The other option mentioned above is the switch statement, where you can have multiple conditions, each with their own action
switch
case condition 1
do some stuff, could be multiple lines of code
break
case condition 2
do some stuff, could be multiple lines of code
break
case condition 3
do some stuff, could be multiple lines of code
break
condition 4
do some stuff, could be multiple lines of code
break
case condition 5
do some stuff, could be multiple lines of code
default
do this stuff if none of the above conditions are met
end switch
Read up on if/then, switch and case in the dictionary for a full explanation of what they can do.
EDIT: added the case preceding the conditions in the switch statement
Last edited by edgore on Mon Aug 26, 2013 5:31 pm, edited 2 times in total.
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: more than one action for if else
Edgore, like most of us, reply while driving to work. Clearly a typo, but know that "case" precedes each conditional line:
switch
case condition 1
do some stuff, could be multiple lines of code
break
case condition 2
do some stuff, could be multiple lines of code
break
case condition 3
...
switch
case condition 1
do some stuff, could be multiple lines of code
break
case condition 2
do some stuff, could be multiple lines of code
break
case condition 3
...
Re: more than one action for if else
Worse - replying in one window, doing email in another window, and working on a script in another window. While on the phone.
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: more than one action for if else
There is also the "else if" construction if you have several "if"s but you aren't using "switch":
Code: Select all
if conditionOne then
act on condition 1
else if conditionTwo then
act on condition 2
else if conditionThree then
act on condition 3
end if
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: more than one action for if else
I keep forgetting that "else if - then" is available in Livecode because it's so much less readable than switch.