CSC 162 Visual Basic I Programming
Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit condition is tested after the body is executed at least once
Pretest Loops While/Wend or Do While/Loop Do Until/Loop
While/Wend or Do While/Loop While condition body_of_code Wend Do While condition body_of_code Loop As long as the condition remains satisfied, the loop will continue to execute. Once the condition is false, the code will resume execution after the Wend or Loop statement.
Example Dim intCounter As Integer Dim intSquare As Integer intCounter = 0 Do While intCounter <= 10 intSquare = intCounter ^ 2 Print intCounter & " squared is " & intSquare intCounter = intCounter + 1 Loop
Do Until/Loop Do Until condition body_of_code Loop As long as the condition is not satisfied, the loop will continue to execute. Once the condition is true, the code will resume execution after the Loop statement.
Example Dim intCounter As Integer Dim intSquare As Integer intCounter = 0 Do Until intCounter > 10 intSquare = intCounter ^ 2 Print intCounter & " squared is " & intSquare intCounter = intCounter + 1 Loop
MsgBox Function intUserChoice = MsgBox("Your average is " & sngAverage, _ vbOkOnly, "Average") MsgBox(Message, Type, Title) The message box is used to display information or to provide the user with a choice. The message box will provide information back to the program about which button the user presses, so it needs to be assigned to an integer variable. Types of message boxes are found in Section (pages 438 – 443).
Class Discussion Page 125 #4.11 Lab Page 125 #4.16