Presentation is loading. Please wait.

Presentation is loading. Please wait.

For…Next and Do...While Loops! Happy St. Patrick’s Day!

Similar presentations


Presentation on theme: "For…Next and Do...While Loops! Happy St. Patrick’s Day!"— Presentation transcript:

1 For…Next and Do...While Loops! Happy St. Patrick’s Day!

2 For…Next Loop?  Allows a block of code to be repeated a certain number of times.  Faster and more efficient.  Uses a Counter Variable that increments each time the loop begins a cycle.  The loop stops when the counter reaches a program-specified number.

3 Syntax The ForNext construct looks like this: For [counter variable] = [start] TO [finish] [do this code] Next [counter variable] Do This Code Next For Counter = Start to Finish If Counter = Finish then Exit

4 Example Program!  Open Visual Basic.  Create a command button.  Double click on the command button and enter the following code: Dim intValue As Integer Dim strName As String For intValue = 1 to 100 strName = InputBox("What is your name?") If strName = "Donald Duck" Then MsgBox "Impossible! Ducks cant type." Exit For End If Next intValue MsgBox ("All done!")

5 Step Parameter! You can add the Step parameter to the ForNext construct to make it increment by values other than 1. For example, to display every other number, you would modify the For statement as follows:  For iNumber = iStart To iFinish Step 2 You could make the construct count backwards, as well:  For iNumber = iFinish To iStart Step 1

6 Things to Remember! There are some things to remember about the ForNext construct:  Unless you specify a negative value for Step, the starting number must be less than or equal to the ending number.  A For...Next construct always performs the code inside the construct at least once. For example, if you specify a start and finish value of zero, the code within the construct will complete one time.

7 DoWhile Loop! Syntax: Do While [comparison] [do this] Loop As long as the comparison returns a True value, the code within the construct will continue to repeat.

8 Tip! If your program gets into an infinite loop, you can press CTRL+BREAK to break Visual Basic out of the loop and stop the program. If your program gets into an infinite loop, you can press CTRL+BREAK to break Visual Basic out of the loop and stop the program.

9 Example: For example, the following DoWhile construct will execute until the user types a value other than 10: Dim intValue As Integer Do While intValue <> 10 intValue = InputBox("Guess a number:") Loop

10 Open Visual Basic – Example Program!  Open Visual Basic.  Create a command button.  Double click on the command button and enter the following code: Dim intVariable intVariable = 101 Do While intVariable > 100 intVariable = InputBox("Enter a number more than 100") If intVariable > 1000 Then MsgBox "1000 is too big!" Exit Do End If Loop MsgBox "All done!"

11  Finish your worksheets.  Quiz tomorrow! On If statements, For…Next, and Do...While Loops. Homework: Study for Quiz! You can use your notes. Only 5 questions!


Download ppt "For…Next and Do...While Loops! Happy St. Patrick’s Day!"

Similar presentations


Ads by Google