Tambien hay una version en castellano de este post, click aca.
Welcome to this post, we’ll talk about the most universal conditional by excellence.
The conditionals bring us the possibility to giving reasoning to our codes. This means that at base of a condition we can choose the addressing of the code, telling what will do and not. Let’s watch its basic syntax:
if condition then
... statements ...
end if
Always starts with if and the condition followed by then and the block is closed with end if. Inside of the block will have the statements. Let’s watch a simple example:
dim rta
rta = inputbox("Do you want to continue (Y/N)?")
if rta = "y" then msgbox "Thanks!"
if rta = "n" then msgbox "See ya later"
In this case, we need to answer a question with yes (y) or no (n) and we store it in a variable. Then, we check with two conditionals if rta is equal to y or n. In both cases, we show a message to indicate each answer. Let’s watch how it works:
On this video, we watch that works each option but when we use another letter it doesn’t show an output. Let’s modify the precedent example:
dim rta
rta = inputbox("Do you want to continue (Y/N)?")
if rta = "y" then
msgbox "Thanks!"
elseif rta = "n" then
msgbox "See ya later"
else
msgbox "You can use only Y or N"
end if
The first change is the replace of one conditional with elseif. This statement is used to add a new conditional to if and evaluates a new condition in reference to the first one. This statement can be used all the times that we need. The second statement added is else. This is used to apply an action when all precedent conditions failed. In this case, we show a message indiating that we can use only those letters. Let’s watch how it works:
It works fine but we’ve an error on the end of video. If we use any of allowed letters but in uppercase the code returns the message of else. Let’s take the precedent example and changes it on the following way:
dim rta
rta = inputbox("Do you want to continue (Y/N)?")
if rta = "y" or rta = "Y" then
msgbox "Thanks!"
elseif rta = "n" or rta = "N" then
msgbox "See ya later"
else
msgbox "You can use only Y or N"
end if
This time we add a logical operator to if and elseif. The or operator brings us the possibility to check a value or another and if one is true executes the block of the conditional. In both cases, we use it to check for the letter in lower and upper case. The rest of code is the same. If you need more information about operators, we talked on this post. Let’s watch how it works now:
Now it works fine and the letter case doesn’t matter. Before we finish, both elseif and else are optional. The elseif statement can be used as many times as necessary but be careful with excess and else can be used only once and is always at the end. Let’s watch how its final syntax is:
if condition then
... statements ...
elseif condition then
... statements ...
elseif condition then
... statements ...
...
elseif condition then
... statements ...
else
... statements ...
end if
The unique mandatory is the first if, the rest are optional. We can use all the elseif that we need and each one has its condition. Finally, we close all with an else for when all above conditions not matched and all ends with end if.
In summary, we talked about if, what it is, how it works, from its most basic syntax until its final syntax, all through an example. I hope you’ve found it useful. You can follow me on this social networks:


Donatión
It’s for site maintenance, thanks!
$1.50
