Visual Basic 2005


Add to MY Favorites...

Back to Vicky's Home Page Teaching Home Page Visual Basic 2005 Html For Kids

Decision Structures

The If..Then Statement

 Example:

If Score >=20 then label1.text = “You win!”

Conditional expression – the result is true or false

Score >=20 is the conditional expression 

If the conditional expression is found to be true, the text You Win! will be assigned to the variable Score.

 

The If..Then..Else Statement

Example:

If Score >=20 then

label1.text = “You win!”

Else

     Label1.text = “Keep trying!”

End if

If the conditional statement is found to be false, the statement following will be executed, and the label will be assigned the text Keep Trying!  When an Else is included, the End if is required as well.  It is a tradition to indent the statement as shown in this example.

 

The If..Then..ElseIf..Else Statement

Example:

If Score >=20 then

label1.text = “You win!”

ElseIF Score >=15 then

     Label1.text = “You did well!”

Else

     Label1.text = “Keep trying!”

End if

 If the conditional statement is found to be false, the statement following will be executed and another IF condition is evaluated.  If the second condition is also false, the assignment following the Else will be performed.  When an ElseIF is used, the two words are joined together.  The End if is required at the end of this as well. 

 

Logical Operators

The logical operators are used to test for multiple conditions.  The logical operators are And, Or, Not and Xor. 

For And, both conditions must be true for the IF to be found true.

For Or, only one of the conditions must be true.

For Not, both must be false for the condition to be true.

For Xor, only one must be false for the condition to be true.

 Example:

IF Age>35 _

And  NaturalBornCitizen = “yes” then

MsgBox(“You can run for president of the US.”)

Else

     MsgBox(“You can’t run for president of the US”)

End If

 

AndAlso and OrElse

These are two new logical operators. The thinking is that these may be helpful when planning for unexpected results that could cause a run-time error. 

Example

IF Age>12 AndAlso age<20 then

     Msgbox(“you are a teenager”)

Else

     Msgbox(“you are not a teenager”)

End if

 

Case

The Case structure begins with the words Select Case and the variable name which will hold the result.  Next follows the Cases; a list of possible conditions with what to assign the variable if the condition is found to be true.  The final Case can be a Case Else which will cover all other possibilities.   The statement ends with an End Select line.  This indenting scheme is commonly used.

 Select Case Greeting

      Case “Birthday”

           Label1.Text = “Happy Birthday”

      Case “Anniversary”

           Label1.Text = “Happy Anniversary”

      Case Else

           Label1.Text = “Have a nice day!”

End Select

 

 
Another page developed by
vicky@vickywoodard.com
Copyright © 2006 Vicky K. Woodard
All rights reserved
Last updated: 8/17/2006