#### Welcome to Synthiam!

The easiest way to program the most powerful robots. Use technologies by leading industry experts. ARC is a free-to-use robot programming software that makes servo automation, computer vision, autonomous navigation, and artificial intelligence easy.

## If Elseif Else Goto & Return Commands

Another one which was written and hidden within a different topic and should warrant it's own tutorial post, the IF (etc.) commands.

#### Quote:

If (Value Condition Value )
IF condition can support multiple comparisons and functions.
Condition tree must be closed with an ENDIF

An if command requires a condition, this is the part in brackets after it. Think of it logically, it's the same as you think when you decide IF you want to do something or not. So, for instance, you go to the supermarket, you walk past the apples and need to decide IF you need any. You check how many you have at home (not sure how, I didn't think this through well but we will carry on as it's unimportant), how many you need and decide.

`Code:IF(\$ApplesAtHome < \$ApplesNeeded) Buy some applesEndIf`

This says, if you have less than the amount of apples you need at home then you need to buy some apples. Otherwise do nothing. Basically, whatever is between the IF and the EndIf lines is only executed IF the condition is met or true. Otherwise it is ignored.

ElseIf works in a similar way but is part of a bigger condition. Say you wanted to know IF you needed to buy a lot apples or needed to buy just a few apples.

`Code:IF(\$ApplesAtHome = 0) Buy lots of applesElseIf(\$ApplesAtHome < \$ApplesNeeded) Buy a few applesEndIf`

You could change it so it works out how many apples to buy with some arithmetic functions but I will save that for another time. One function at a time is easiest to learn.

You can have as many ElseIfs as you need. It's only the part between the first ElseIf and the next ElseIf line or EndIf (if it's last) that is executed, the rest is ignored.

Now say only needed apples provided you had no bananas, this is where the AND and OR conditions come in to play. Again, think logically. If I have no bananas and have no apples then I need some apples...

`Code:IF(\$BananasAtHome = 0 and \$ApplesAtHome < \$ApplesNeeded) Buy applesEndIf`

Only if both parts of the condition are met will the command between the IF and EndIf be executed.

Or is another condition that can be used. For instance, you now look at the pears, but you only need to buy pears IF you have no apples or have no bananas.
`Code:IF(\$apples = 0 OR \$bananas = 0) Buy some pearsEndIf`

All of the AND, OR, <, >, =, <=, >= and <> conditions can be used in all IF statements and all ElseIf statements. You can also mix and match the ANDs and ORs. Phrase the condition correctly, in the order you would phrase it to a person or in your head...
`Code:IF(\$apples = 0 OR \$bananas = 0 AND \$pears < 5) Buy some pearsElseIf(\$apples = 0 AND \$bananas = 0 or \$Apples = 0 AND \$pears < 2) Buy a mangoEndIf`
In this example, if you have no bananas or apples and less than 5 pears you will buy some pears. If you have no apples and no bananas, or no apples and less than 2 pears you will buy a mango. Otherwise you buy nothing.

Else is slightly different, else doesn't have a condition. Think of the last example and when you buy nothing. Else fills in this gap. Rather than buying nothing, or executing nothing, if no condition is met something else can be executed or bought.
`Code:IF(\$apples = 0 OR \$bananas = 0 AND \$pears < 5) Buy some pearsElseIf(\$apples = 0 AND \$bananas = 0 or \$Apples = 0 AND \$pears < 2) Buy a mangoElse You have too much fruit, buy beerEndIf`
In this example, if you have no bananas or apples and less than 5 pears you will buy some pears. If you have no apples and no bananas, or no apples and less than 2 pears you will buy a mango. Otherwise you buy beer.

Goto and Return

#### Quote:

Goto( label )
Goto a specific :Label location

#### Quote:

Return()
Return from a Goto()
If you jump to a position of code with a Goto(), the Return statement will allow you to return back to that piece of code following the last Goto()
If you attempt to Return() with an empty stack, nothing will happen. The script will ignore the Return() statement.

The Goto command is one I use a lot as part of IF statements. Rather than re-writing the same code over and over I set these up as smaller "sub-routines" and call them with Gotos, returning back once run with the Return command.

Following on the same examples. Your wife tells you to GOTO the shop
`Code:Goto(shop)`

So you go to the shop. Or in the script, jump to the label called shop.
`Code::shop`

You then carry on following whatever instructions are after that label. Let's say it's the above IFs. You keep following line by line ignoring anything that any IF conditions are not met.

At the end of it you need to RETURN home. So a return command is used.
`Code:Return()`

This command will go back the last Goto command which was executed, and then you carry on following instructions line by line.

So, an example with line numbers;
`Code:1 Print("Hello world")2 If(\$day = "Monday")3 Goto(monday)4 ElseIf(\$day = "Friday")5 Goto(friday)6 ElseIf(\$day = "Saturday" or \$day = "Sunday")7 Goto(weekend)8 Else9 Goto(end)10 EndIf11 Halt()12 :monday13 Print("It is Monday, Boo!")14 Return()15 :friday16 Print("It is Friday, the weekend is almost here!")17 Return()18 :weekend19 Print("It is the weekend, yay!")20 Return()21 :end22 Print("Today isnt important - terminating")23 Halt()`

In this example code, if it's a Monday it will execute the lines of code in this order;
1, 2, 3, 12, 13, 14, 10, 11

If it's a Friday it will execute in this order;
1, 2, 4, 5, 15, 16, 17, 10, 11

If it's a weekend;
1, 2, 4, 6, 7, 18, 19, 20, 10, 11

If it's any other day;
1, 2, 4, 6, 8, 9, 21, 22, 23

Take a look at my Ping Roam script for some IF tree examples which use Gotos and Returns (and in some cases need Gotos in order to return). They are well documented so should prove to be useful examples.

As always, any questions, comments, suggestions etc. are more than welcome.

Become a Synthiam ARC Pro subscriber to unleash the power of easy and powerful robot programming

#1
Thanks Rich! Crystal clear tutorial!.....and using references we ***ALL*** can understand ...food ...beer..and wife(GF)
Australia
#2
Hi,
This is my first post

I have been doing some reading on the forums to help me decide to buy an EZ robot to get my son into robotics ,
With this kind of support and tutorial , I will definitely be buying A roll soon

Hais.
United Kingdom
#3
Awesome, you have no idea how that makes me feel! That right there is why I do what I do You won't have any regrets getting an EZ-B powered robot.

FYI, you can download ARC now and play, write scripts etc. although it's much "funner" if something moves while you are learning, but it's a start while waiting
#4
@Rich- Great tutorial. @irobot said it best, crystal clear. And, I see you got another commission check coming. LOL.

@haris- That's just a little joke. Rich does not get any compensation if you purchase an EZ-Robot product. We'll just say you're not the first person to say, "Thanks Rich, I'm going to order an EZ-Robot now." I just wanted to be clear on that. And like Rich said, get ARC from the download page, grab up your boy and play around with it.
United Kingdom
#5
You know the feeling you get from helping and making a difference is good enough for me
#6
Thanks for this Rich... I always thought a comand like "Continue" would be really useful... So once a condition returns true the "Continue" comand could be used to skip to the "repeat" part of the loop, bypassing all other statements..
Australia
#7
Hi ,

Yea , the help and support i can see you are providing is awesome , it helps newbies thinking about getting into this stuff,
Sometimes it feels very intimidating to get started , but seeing this support does make a difference.

i.e. seeing ...
Code:
IF(\$apples = 0 OR \$bananas = 0 AND \$pears < 5)
ElseIf(\$apples = 0 AND \$bananas = 0 or \$Apples = 0 AND \$pears < 2)