VBS_6 – Decisions

Note: The segments of code below are not full running scripts.  You need to combine the variables, input boxes, …etc from before in order to use the code segments below.

A very important task in programming is to be able to make decisions.  These are also called conditionals.  Three types of conditionals that you should become familiar with are:

  • If Statement – executes a set of code when a condition is true
  • If … Then … Else statement – allows for two different scenarios to determine which code is excecuted
  • If … Then … ElseIf statement – allows the program to select one of many sets of code to execute

If Statement
Use an If statement when you only want to execute a block of code ONLY when the condition is TRUE. 
Example:

If numberInteger > 100 Then
         msgbox “Your number is greater than 100.”
End If

If … Then … Else
Use an If …. Then … Else statement if you want to run alternate blocks of code, depending on if the condition is TRUE or FALSE.
Example:

If numberInteger > 100 Then
         msgbox “Your number is greater than 100.”
Else
        msgbox “Your number is 100 or less.”
End If

If … Then … ElseIf
Use the If … Then … ElseIf statement when you have MANY possible conditions that could be true.
Example:

If numberInteger > 100 Then
           msgbox “Your number is greater than 100.”
ElseIf numberInteger > 50 Then
           msgbox “Your number is greater than 50.”
ElseIf numberInteger <50 Then
           msgbox “Your number is 50 or less.”
End If

____________________________________________________________________________________________________

 ASSIGNMENT

After working through the above examples…

I would suggest making a Mad Lib … lots of fun
http://www.madglibs.com/

Use Notepad++ or some such program and create a script of your own ideas that:

  • Has a message box telling the user what the program does.
  • At least two variables (You will probably have lots more: verb1, noun1, …).
  • At least one input box to get a variable value from the user (You will have lots more).
  • A decision using one of the above conditionals (A common example is to check the users name against your own and have something like: “That is a great name!” if they put your name in).
  • At least one message box to display a result (The Mad Lib).
  • At least one comment (Your name should be at the top).

Save your script file and run it to test it.  Then hand in your code as a .txt file.

____________________________________________________________________________________________________

Leave A Reply

Your email address will not be published. Required fields are marked *