Script flow functions - If..Then Statements

Top  Previous  Next

Introduction

imageformanual

Making decisions is what if statements are all about. By reading road signs you'll adjust the speed or direction of your vehicle. Hitting the ball in the right direction will make a goal for your team. Adding extra pepper will make your meal spicey.

 

IfStatementCardHouse

 

As you can see from the image above, depending on what you put on the house of playing cards - that house will either keep standing or collapse.

MMB uses variable label as platform for variable value (object you put on the platform).

 

Basic If statement syntax

 

Script code lines consist of:

a) writing If clause:

If

b) opening parenthesis with variable label:

(object$

c) followed by equal sign:

=

d) then writing contents (value) of variable at the end, behind label and equal sign, followed by closing parenthesis:

'rock')

e) and adding of then clause at the end of the first line:

Then

 

Put together, first line of if statement will look like this:

If (object$='rock') Then

This line instructs MMB to perform certain actions if content of string variable object$ matches exactly the string specified inside single quotes (in this case: rock).

And what if variable really matches specified content ? After the line above, you will continue writing script lines that will be executed in the case of content match:

MyText$='Object put to platform is: '+object$

Message("","MyText$")

When you're done writing code lines that should be performed in match case, you can use another clause:

Else

...and continue writing lines that should be performed if variable doesn't match specified content. For example:

MyText$='Unknown object has been put to platform'

Message("","MyText$")

Adding of else clause and script lines of else case are optional. But, whatever you decide, you must tell MMB when you're done with if statement, by adding the end clause:

End

 

Let's see now how entire if statement code looks like !

 

If (object$='rock') Then

 MyText$='Object put to platform is: '+object$

Message("","MyText$")

Else

MyText$='Unknown object has been put to platform'

Message("","MyText$")

End

 

Translated: if content of string variable object$ is the rock, text assigned to MyText$ string variable will be "Object put to platform is: rock" and it'll be displayed in MMB's message box.

But if content of string variable object$ is something else, text assigned to MyText$ string variable will be "Unknown object has been put to platform" and it'll be displayed in MMB's message box. All lines put after Else clause will be executed only when If clause is false.

 

Of course, you don't have to use Else if you don't want to:

If (object$='rock') Then

 MyText$='House of cards has collapsed !'

Message("","MyText$")

 Return()

End

 

These code lines will display message box with text "House of cards has collapsed !" if object$ variable contents is rock , and after that it'll call Return() command to stop going any further in script code lines (both in & out of if statement code lines).

 

Multiple If statements

 

Another option is to use multiple If statements, to make more decisions:

 

If (TankVolume=10) Then

Message(" Please add some more fuel ","")

 Return()

End

If (TankVolume=50) Then

Message(" Tank is half full ","")

 Return()

End

If (TankVolume=100) Then

Message(" Tank is full ! ","")

 Else

 Message("Tank status unknown","")

End

 

The first thing you'll notice above is - we're using numerical variables. The principle is completely the same as for string variables. Here we take numerical variable TankVolume that contains some value. Once it goes through multiple if statements, we'll have message box pop up even when neither of if cases is true, because last if statement also includes Else clause, containing the code that will be executed if code lines didn't go in any other direction.

 

Using value range in If statements

 

It wouldn't be very handy to have only fixed values in if statements:

 

IfBottles1

 

We would need too many if cases to handle all sub-values. This looks better:

 

IfBottles2

 

So, not only equal operator is available, but variety of others:

 

Operator

Name

=

equals

<>

different than

<

less than

>

greater than

<=

less than or equal

=>

more than or equal

 

Put into code lines, here's how these operators look like:

If (Volume<10) Then

Message("Volume is less than 10 !","")

End

(displays message box if content of numerical variable Volume is under 10)

If (Volume<=49) Then

Message("Volume is under 50.","")

End

(displays message box if content of numerical variable is 49 or under that number)

If (Volume<>100) Then

Message("Volume is not 100.","")

End

(displays message box if content of numerical variable is different than 100)

 

Multiple cases in If statements

 

Another neat feature of if statements in MMB is ability to use more conditions in one line.

 

IfBottles3

 

There are two available operators:

 

Operator

Name

&

and

|

or

To use these, you'll start by writing an If clause:

If

...opening parenthesis with first variable label:

(OldUser$

...followed by equal sign:

=

...then writing content (value) of variable at the end, behind label and equal sign:

'John'

...and adding either & or | operator:

&

After writing this, you'll start adding the next case, without opening new parenthesis, but continuing writing variable name inside current one:

NewUser$

...followed by equal sign:

=

...and writing contents (value) of variable at the end, behind label and equal sign:

'Peter'

Now you can either continue adding another case, or finish the if statement code line by closing parenthesis:

)

... and adding Then clause at the end of the first line:

Then

Put together, first line of If statement with more conditions looks like this:

 

If (OldUser$='John' & NewUser$='Peter') Then

 

And what will this code line do ? Check value of OldUser$ for a match with name John. If the match exists, it'll go and check if value of NewUser$ matches name Peter. If that's the case, MMB will perform code lines entered after Then clause.

Here's an example with | (or) operator:

 

If (OldUser$='John' | NewUser$='Peter') Then

 

Case will be positive if either OldUser$ or NewUser$ matches the case, and MMB will perform code lines entered after Then clause. If neither match, if statement code lines are ignored.

 

And there are even more combinations here ! Variables can be compared with other variables, multiple cases can be combined between numerical and string variables, using of <> operator is available not only for numerical, but also for string variables. Nested if statements (one in another) are also available here.

 

Let's see how these features work, one by one...

 

Variable vs. Variable

 

In many occasions, values are stored in variables. It's possible to compare two variables in the if statements and check do they match:

 

If (User1$=User2$) Then

Message("Users are identical !","")

End

 

Example above compares 2 string variables (User1$, User2$) and if they match, message box is displayed, saying "Users are identical !". Here's one example that uses numerical variables:

 

If (CurrentLevel>Recommended) Then

Message("Level is higher than recommended !","")

End

 

Example checks if value of numerical variable CurrentLevel is higher than value of numerical variable Recommended. If it is, message box is displayed, saying "Level is higher than recommended !"

 

If statements with mixed variables

 

Another advantage of if statements with multiple cases is ability to look for matches on both string and numerical variables inside the same if statement. Here's an example:

 

If (UserName$='Bundy' & UserHeight>197) Then

Message("Bundy is higher than 197 cm","")

End

 

Code above checks match on 2 variables - string variable UserName$ and numerical variable UserHeight. If string variable contains name Bundy, and if numerical variable content is higher than 197, message box will display: "Bundy is higher than 197 cm".

 

Checking match with <>

 

In some cases you won't be interested in checking value range or match, but only see if either numerical or string variable differs from specified value. One of usual examples is checking of passwords, where your program only wants to know if user input matches (either variable or fixed) password. So let's see how this works:

 

If (UserInput$<>'SoftwareRegKey') Then

Message("Entered software reg key is not valid !","")

End

 

You remember <> operator from previous paragraphs - and it's by default used for numerical variables. But in MMB you can use <> to search for a difference on string variables too.

Just for the record, here's an example with numerical variable:

 

If (MyHeight<>YourHeight) Then

Message("We have different height !","")

End

 

Here we check the difference between two numerical variables. If difference exists, message box is displayed, saying: "We have different height !".

 

Nested If statements

 

In addition to multiple if statements, here's another feature that is actually longer version of multiple case if statement. It's possible to enter second if statement inside of the first one and have parts of the code executed depending on case match.

 

If (UserName$='Bundy') Then

 If (UserHeight>197) Then

    Message("Bundy is higher than 197 cm","")

 End

End

 

Although you could replace this example with shorter version:

 

If (UserName$='Bundy' & UserHeight>197) Then

Message("Bundy is higher than 197 cm","")

End

 

...by using nested If statements it's possible to execute some other code inside of the first statement, that doesn't depend on the second if statement match case:

 

If (UserName$='Bundy') Then

Message("Hello Bundy !","")

 If (UserHeight>197) Then

    Message("Bundy is higher than 197 cm","")

 End

End

 

Code lines above will check if string variable UserName$ matches name "Bundy". If it does, message box with "Hello Bundy !" text is displayed. After that, second (nested) if statement is checked with numerical variable UserHeight. If number is higher than 197, second message box will be displayed, saying: "Bundy is higher than 197 cm".

So, even if Bundy is not higher than 197 cm, first (Hello Bundy !) message box will be displayed. The same principle goes for code lines entered below entire second if statement.

Notice where nested if statements are entered - End clause of previous if statement is not put above 'em, but below, encapsulating nested statements. You can add as many nested statements as you like and brake further execution with Return() command:

 

If (UserName$='Bundy') Then

Message("Hello Bundy !","")

If (UserHeight>197) Then

    Message("Bundy is higher than 197 cm","")

  If (UserWeight>110) Then

        Message("Bundy weighs more than 110 kilos","")

                  Return()

    End

    Message("Bundy weighs less than/or 110 kilos","")

End

End

 

If statements are marked with different colors so you can see which End clause belongs to which statement. First message box is displayed if UserName$ matches name "Bundy". Second message box is displayed if numerical variable UserHeight contains number higher than 197. Now, third if statement (marked red) is interesting - if numerical variable UserWeight is higher than 110, message box will display "Bundy weighs more than 100 kilos" and that's where execution of all if statements is stopped with Return() . But if UserWeight is not greater than 110, message box will display "Bundy weighs less than/or 110 kilos".