0 index
1 Conditions
2 If - Then - Else
3 Syntax
4 Select Case
5 Syntax
6 Example
7 Do..Loop
8 Syntax
9 WHILE Condition
10 UNTIL Condition
11 Placement of the Condition
12 Loop
13 Example: Until at bottom
14 Example: While at top
15 Example: While at bottom
16 3 things to consider
17 For/Next Loop
18 Syntax:
19 Example: Simple counting loop
20 Example: Counting backwards
21 Using the counter inside the loop
22 Other techniques you will need
23 Print method

outline
created using slideshow.cgi by Andy Harris















CSCI N331 Visual Basic: n331/ConditionsandLooping
1. Conditions
  • Conditions are expressions that can be evaluated as true or false.
  • Conditions are NOT an equation.
  • Conditions usually compare a value to a variable - can also be a variable to a variable.



































CSCI N331 Visual Basic: n331/ConditionsandLooping
2. If - Then - Else
  • This is the most basic condition
  • This is used for simple branching



































CSCI N331 Visual Basic: n331/ConditionsandLooping
3. Syntax
  • Simple form: IF expression Then command
    If userName = "Wally" Then MsgBox("Hello, Wally")
  • Block form:
    IF expression Then
       code
    ELSE
       more code
    END IF



































CSCI N331 Visual Basic: n331/ConditionsandLooping
4. Select Case
  • Select Case is used for multiple branching
  • Tests one variable for multiple values



































CSCI N331 Visual Basic: n331/ConditionsandLooping
5. Syntax
  • SELECT CASE variable
       CASE value 1
         Basic statement
       CASE value 2
         Basic statement
       CASE ELSE
         Basic Statement
       END SELECT



































CSCI N331 Visual Basic: n331/ConditionsandLooping
6. Example
  • Dim Year as String
    Year = InputBox "In which year was 'Gone with the Wind' produced?"
    Select Case Year
        Case "1937"
          MsgBox "WAY too early"
        Case "1938"
          MsgBox "A little too early"
        Case "1939"
          MsgBox "PERFECT!!!"
        Case Else
          MsgBox "Your are WAY off!!"
    End Select



































CSCI N331 Visual Basic: n331/ConditionsandLooping
7. Do..Loop
  • This is used for conditional looping
  • DO and LOOP keywords mark the beginning and the end of the loop.



































CSCI N331 Visual Basic: n331/ConditionsandLooping
8. Syntax
  • DO [WHILE, UNTIL, or NOTHING] [Condition for WHILE or UNTIL]
        code inside loop
    LOOP [WHILE, UNTIL or NOTHING] [Condition for WHILE or UNTIL]



































CSCI N331 Visual Basic: n331/ConditionsandLooping
9. WHILE Condition
  • The loop continues as long as the condition is TRUE
  • The loop ends when the condition is FALSE



































CSCI N331 Visual Basic: n331/ConditionsandLooping
10. UNTIL Condition
  • The loop continues as long as the condition is FALSE
  • The loop ends when the condition is TRUE
  • This is the opposite of WHILE



































CSCI N331 Visual Basic: n331/ConditionsandLooping
11. Placement of the Condition
  • DO - the condition is evaluated BEFORE the code is run
  • It is possible to skip code
  • Is usually necessary to initialize a sentry variable
    - a sentry variable is a counter type variable



































CSCI N331 Visual Basic: n331/ConditionsandLooping
12. Loop
  • Code runs at least once before condition is checked
  • The initialization often occurs in code
  • Is more commonly used



































CSCI N331 Visual Basic: n331/ConditionsandLooping
13. Example: Until at bottom
  • Dim Answer as Integer
    Answer = 0
    Do
         Answer = Val(InputBox ("What is 2+3"))
    Loop Until Answer = 5



































CSCI N331 Visual Basic: n331/ConditionsandLooping
14. Example: While at top
  • Dim At_Peace as Boolean
    At_Peace = True
    Do while At_Peace = False
         Launch_Missles(Russia)
    Loop



































CSCI N331 Visual Basic: n331/ConditionsandLooping
15. Example: While at bottom
  • Dim At_Peace as Boolean
    At_Peace = True
    Do
         Launch_Missles (Russia)
    Loop while At-Peace = False



































CSCI N331 Visual Basic: n331/ConditionsandLooping
16. 3 things to consider
  • What is the condition?
  • Do I need the condition at the top or the bottom?
  • Do I want a while or until condition?



































CSCI N331 Visual Basic: n331/ConditionsandLooping
17. For/Next Loop
  • This is a simpler and older loop
  • Used for counting
  • You must know how many times you will do the task (either directly or in a variable)



































CSCI N331 Visual Basic: n331/ConditionsandLooping
18. Syntax:
  • For counting_variable = 1 to limit [STEP steps]
         BASIC code?..
    NEXT counting_variable



































CSCI N331 Visual Basic: n331/ConditionsandLooping
19. Example: Simple counting loop
  • For I = 1 to 10
    MsgBox Str (I)
    NEXT I



































CSCI N331 Visual Basic: n331/ConditionsandLooping
20. Example: Counting backwards
  • For I = 10 to 1 Step -1
         MsgBox Str (I)
    Next I



































CSCI N331 Visual Basic: n331/ConditionsandLooping
21. Using the counter inside the loop
  • For Xpos = 0 to MyForm.Width Step 100
         ImgCar.Left = Xpos
    Next XPos



































CSCI N331 Visual Basic: n331/ConditionsandLooping
22. Other techniques you will need
  • CLS Method
  •    Atached to picture boxes and forms
  •    Allows you to clear the object
  •    Derived from the old CLS command
  • Syntax: ObjectNaame.cls



































CSCI N331 Visual Basic: n331/ConditionsandLooping
23. Print method
  • Attached to picture boxes, forms and printer object
  • Allows you to print a value directly to the object
  • Derived from old PRINT command
  • Syntax: ObjectName.Print Value
  • Value can be a string or numeric, direct or variable name



































outline

Conditions

If - Then - Else

Syntax

Select Case

Syntax

Example

Do..Loop

Syntax

WHILE Condition

UNTIL Condition

Placement of the Condition

Loop

Example: Until at bottom

Example: While at top

Example: While at bottom

3 things to consider

For/Next Loop

Syntax:

Example: Simple counting loop

Example: Counting backwards

Using the counter inside the loop

Other techniques you will need

Print method