more than one action for if else

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Beginner
Posts: 6
Joined: Thu Aug 15, 2013 7:53 am

more than one action for if else

Post by Beginner » Mon Aug 26, 2013 1:40 pm

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.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: more than one action for if else

Post by Dixie » Mon Aug 26, 2013 1:46 pm

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

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Mon Aug 26, 2013 2:37 pm

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

Beginner
Posts: 6
Joined: Thu Aug 15, 2013 7:53 am

Re: more than one action for if else

Post by Beginner » Mon Aug 26, 2013 3:34 pm

Oh I see, so in Livecode the indentations and paragraphing does matter,
I think I'm ok for now, thanks!

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: more than one action for if else

Post by edgore » Mon Aug 26, 2013 4:01 pm

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
Last edited by edgore on Mon Aug 26, 2013 5:31 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Mon Aug 26, 2013 4:18 pm

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

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: more than one action for if else

Post by edgore » Mon Aug 26, 2013 5:30 pm

Worse - replying in one window, doing email in another window, and working on a script in another window. While on the phone.

jacque
VIP Livecode Opensource Backer
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

Post by jacque » Mon Aug 26, 2013 5:54 pm

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

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

Re: more than one action for if else

Post by edgore » Mon Aug 26, 2013 7:22 pm

I keep forgetting that "else if - then" is available in Livecode because it's so much less readable than switch.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”