Page 1 of 1
more than one action for if else
Posted: Mon Aug 26, 2013 1:40 pm
by Beginner
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.
Re: more than one action for if else
Posted: Mon Aug 26, 2013 1:46 pm
by Dixie
Do something like...
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
Have a look at 'switch' in the dictionary if you have lots of choices to consider
Re: more than one action for if else
Posted: Mon Aug 26, 2013 2:37 pm
by dunbarx
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
Re: more than one action for if else
Posted: Mon Aug 26, 2013 3:34 pm
by Beginner
Oh I see, so in Livecode the indentations and paragraphing does matter,
I think I'm ok for now, thanks!
Re: more than one action for if else
Posted: Mon Aug 26, 2013 4:01 pm
by edgore
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
Re: more than one action for if else
Posted: Mon Aug 26, 2013 4:18 pm
by dunbarx
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
...
Re: more than one action for if else
Posted: Mon Aug 26, 2013 5:30 pm
by edgore
Worse - replying in one window, doing email in another window, and working on a script in another window. While on the phone.
Re: more than one action for if else
Posted: Mon Aug 26, 2013 5:54 pm
by jacque
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
Re: more than one action for if else
Posted: Mon Aug 26, 2013 7:22 pm
by edgore
I keep forgetting that "else if - then" is available in Livecode because it's so much less readable than switch.