FANDOM


The If Statement is a tool to compare something to another something. It can be used for checking the value from message boxes and input boxes, comparing two variables, strings, numbers, et cetera.

SyntaxEdit

There are 6 main parts to the if statement (The If, the comparison, the Then, the ElseIf, the Else, and the End If). The If always begins the statement. Then you do the comparison - if you say it out loud, it would be something like

"If this thing is (equal, greater than, or less than) this other thing, then do this. If it's not that, then is this (equal, greater than, or less than) this? If so, do this. If it's none of those, then do this."

That probably didn't make much sense...so maybe it will with a look at the code. Below is the format of the if statement.

If Comparison1 = Comparison2 Then
     MsgBox "Do this if it's true"
ElseIf Comparison3 > "Comparison4" Then
     MsgBox "Do this if the initial comparison is false, and this comparison is true"
ElseIf Comparison5 < Comparison6 Then
     MsgBox "Do this if the first two comparisons were false, and this one is true"
Else
     MsgBox "If none of the comparisons above were true, then do this."
End If

In the above example, the [=] and [>] and [<] signs could have been replaced with any of the three (i.e. they could have all been equal signs, or all greater than signs, or all less than signs, or mixed and matched to your needs).

Also, you don't need the ElseIf's and the Else statement...if you just want to compare something to something else, then you don't need them...such as in the following example.

If Comparison1 = Comparison2 Then
     MsgBox "Do this if it's true.
End If

If you wanted to limit it down further, you don't always need the End If either! If you can fit the "To do if it's true" into one line, like I did in the above example, you can just have the entire if statement on one line without the End If, like so:

 If Comparison1 > Comparison2 Then MsgBox "Comparison1 is greater."

Hint: You can have any number of ElseIf's in an if statement, from 0 to infinity.

Here is an example of the if statement comparing two variables to see which contains the greater number. Var1 is set to 10, and Var2 is set to 50. It will then display a message box telling you which is greater.

Var1 = 10

Var2 = 50

If Var1 > 50 Then

     MsgBox "Variable 1 contains a larger number than variable 2."

Else

     MsgBox "Variable 2 contains a larger number than variable 1."

End If

There are several different kinds of comparisons youc an do with the if statement. The one shown above is the Greater Than [>] comparison. You can also use the Less Than [<], or the Equals [=]. Here is an example using the less than sign, comparing amount of money (Variable name is 'Money') to the minimum required amount of money (Variable name is 'Required').

Money = 15
Required = 10
If Money < Required Then
     MsgBox "Sorry, save up more money and come back again."
Else
     MsgBox "You have enough money to purchase this item."
End If

Related:Edit

Variables

MsgBox (Function)



Remember, if you ever need help, you can ask BusyCityGuy on his talk page.


Did you find what you needed?
 
35
 
16
 
18
 

The poll was created at 23:16 on June 10, 2012, and so far 69 people voted.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.