Download presentation
Presentation is loading. Please wait.
Published byMuriel Carson Modified over 9 years ago
1
Controlling Program Flow with Looping Structures
Chapter 5 Controlling Program Flow with Looping Structures
2
The Do…Loop Statement Is a looping structure that executes a set of statements as long as a condition is True. Iteration – repeating one or more statements Example: Dim intNum As Integer intNum = 0 Do intNum = intNum ‘Increment by 2 Loop While intNum < 10 (Do…Loop executes five times, on the fifth time intnum = 10 making the condition false)
3
Do…Loop While The code in the loop must be executed at least once
The condition determines if the loop will be repeated (True it repeats False it leaves the loop and continue on with the program). Example: Dim intNum As Integer intNum = 0 Do while intNum < 10 intNum = intNum ‘Increment by 2 Loop
4
Do While …Loop Different than the Do…Loop While
The condition is evaluated first before entering the loop. If condition evaluates true the loop is entered. If the condition evaluates to false the loop is skipped. The code in the loop is not guarantee to be execute at all.
5
Infinite Loops Is a loop that does not stop running and never becomes false. CTRL + BREAK will stop an infinite loop if one occurs
6
Input Boxes Is a Visual Basic predefined dialog box that has a prompt, a text box, and OK and Cancel buttons. Used to get information from the user.
7
Input Boxes The InputBox form is InputBox(prompt, title)
Prompt represents a string Title represents a string The InputBox will return a string so you will need to have a String memory variable to hold it or you can display it with the label.
8
Example of Code for the InputBox
Dim strTextInput As String strTextInput = InputBox(“Enter Text”, “Input Box”) Or lblLabel.Caption = InputBox(“Enter text”, “Input Box”)
9
Accumulators Is a numeric variable used to store a value that accumulates, or gets added to, during runtime. Example: intAccum = intAccum + value Flag or Sentinel Values are used to end a loop usually -1 or -999. Example: intNum = 0 Do while intNum <> -1 intNum = txtBox.text Loop
10
For…Next Statement Is a looping structure that executes a set of statements a fixed number of times. For…Next executes until a counter reaches an ending value. Unlike the Do …Loop that executes while a condition is true.
11
For…Next Statement Form
For counter = start To end Statements Next counter Counter, start, and end are Integer variables, values, or expressions.
12
Components of a For…Next loop
Counter is initialized to start only once when the loop first executes. Counter is compared to end before each loop iteration. Counter is automatically incremented by 1 after each iteration of the loop body.
13
Example of a For…Next Loop
Dim intNum As Integer For intNum = 1 To 10 MsgBox intNum ‘Display message box Next intNum ‘Displays message box 10 times
14
Step in a For…Next Loop Step – changes the way the For…Next Loop is incremented Example: For intCount = 2 To 8 Step 2 Statements Next intCount ‘Increments the counter by 2 ‘You can decrement a counter by using negative numbers for the Step value
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.