Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.

Similar presentations


Presentation on theme: "CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which."— Presentation transcript:

1 CHAPTER SIX LOOPS © Prepared By: Razif Razali 1

2 FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which one is better? State your reasons 2 © Prepared By: Razif Razali

3 CONTENTS While For Do/Loop While and Do While/Loop Do/Loop Until and Do Until/Loop 3 © Prepared By: Razif Razali

4 I NTRODUCTION Visual Basic allows a procedure to be repeated many times as long as the processor until a condition or a set of conditions is fulfilled. This is generally called looping. The process of repeating a series of statements multiple times until a criteria is met Looping is a very useful feature of Visual Basic because it makes repetitive works easier. 4 © Prepared By: Razif Razali

5 R EPETITION S TRUCTURES Visual Basic provides six types of repetition structures for performing a statement or group of statements repeatedly: While...Wend For...Next Do While...Loop Do Until...Loop Do...Loop While Do...Loop Until 5

6 W HILE..W END S TATEMENT If condition is True, all statements in statements are executed until the Wend statement is encountered. Control then returns to the While statement and condition is again checked. If condition is still True, the process is repeated. If it is not True, execution resumes with the statement following the Wend statement. 6 © Prepared By: Razif Razali

7 W HILE... W END S TATEMENT A While...Wend statement behaves like the Do While...Loop statement. The following While...Wend counts from 1 to 10 Dim number As Integer number = 1 While number <=10 Print number number = number + 1 Wend 7 © Prepared By: Razif Razali

8 E XERCISE 1 Determine the output for the following VB program 8 © Prepared By: Razif Razali

9 F OR..N EXT L OOP With a For...Next loop, you can execute a specific group of program statements in an event procedure a specific number of times. This can be useful when you want to perform several related calculations, work with elements on the screen, or process several pieces of user input. A loop where the number of iterations is determined by a range of values for a numeric variable Syntax: For controlVariable = initial To terminal statement(s) Next controlVariable 9 © Prepared By: Razif Razali

10 E XAMPLE 1: F OR..N EXT Private Sub cmdDisplayTable_Click() Dim i As Integer ‘Display a table of the first 5 numbers and their squares For i = 1 To 5 picTable.Print i; i ^ 2 Next i End Sub 10 © Prepared By: Razif Razali

11 E XAMPLE 2: F OR..N EXT Dim numVar As Integer For numVar = 1 To 5 Step 2 picTable.Print numVar; Next numVar 11 © Prepared By: Razif Razali

12 W HEN A F OR STATEMENT IS ENCOUNTERED ? The control variable is assigned the initial value. After each loop iteration, the step value is added to the value of the control variable. (If there is no step value, 1 is added.) Iteration continues until the terminating value is exceeded. Each For loop must end with a Next statement 12 © Prepared By: Razif Razali

13 E XAMPLE 3: F OR..N EXT Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next i End Sub 13 © Prepared By: Razif Razali

14 E XAMPLE 4: F OR..N EXT Dim numVar As Integer For numVar = 8 To 1 Step -2 picOutput.Print numVar; Next numVar 14 © Prepared By: Razif Razali

15 N ESTED L OOPS In some cases, it is convenient to use a loop contained within another loop. Such loops are called nested loops. The syntax nested for loop For outer = 1 To 4 ` The outer loop For inner = 1 To 2 ` the inner loop.. Next inner Next outer Notice that all statements in the inner loop are contained within the boundaries of the outer loop. 15 © Prepared By: Razif Razali

16 E XAMPLE 5: D ISPLAY A 10 X 10 RECTANGLE OF STARS Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next i End Sub 16 © Prepared By: Razif Razali

17 E XERCISE 2 By using the for loops, write a VB statement to have the following output: 17 © Prepared By: Razif Razali

18 U SING THE EXIT FOR S TATEMENT Use an Exit For statement to exit a For...Next loop before the loop has finished executing. With this capability, you can respond to specific events that occur before the loop runs the preset number of times. For example, in this For...Next loop, the loop prompts the user for 10 names and prints them on the form, unless the user enters the word Done. (If the user does enter Done, the program jumps to the first statement that follows the Next statement): 18 © Prepared By: Razif Razali

19 U SING THE EXIT FOR S TATEMENT For i = 1 To 10 InpName = InputBox("Type a name or Done to quit.") If InpName = "Done" Then Exit For Print InpName Next i As this example shows, you can use If statements with Exit For statements. You’ll find this combination useful for handling special cases that come up in a loop, and you'll probably use it often. 19 © Prepared By: Razif Razali

20 U SING THE EXIT FOR S TATEMENT You can include any number of Exit For statements anywhere in a For loop. One use of Exit For is to test for a condition that could cause an endless loop, which is a loop that could run a very large or even infinite number of times. If you detect such a condition, you can use Exit For to escape the loop. 20 © Prepared By: Razif Razali

21 W RITING D O L OOPS Do loops are valuable because occasionally you can’t know in advance how many times a loop should repeat. As an alternative to a For...Next loop, you can write a Do loop that executes statements until a certain condition in the loop is true. For example, you might want to let the user enter names in a database until the user types Done in an input box. In that case, you could use a Do loop to cycle indefinitely until the user enters the text string, Done. 21 © Prepared By: Razif Razali

22 T HE D O L OOP The formats are a) Do While condition Block of one or more VB statements Loop b) Do Block of one or more VB statements Loop While condition c) Do Until condition Block of one or more VB statements Loop d) Do Block of one or more VB statements Loop Until condition 22 © Prepared By: Razif Razali

23 D O W HILE L OOP Is executed as long as the condition is True. If condition is False then the next statement after the Loop is executed. Do While Syntax Do While condition is true statement(s) Loop 23 © Prepared By: Razif Razali

24 E XAMPLE 1 : D O W HILE Private Sub cmdDisplay_Click() Dim num As Integer ' Display the numbers from 1 to 10 num = 1 Do While num <= 10 picNumbers.Print num; num = num + 1 Loop End Sub 24 © Prepared By: Razif Razali

25 C ONTROLLING L OOPS Methods of controlling loops: Counter-controlled loops repeat a specific number of times Event-controlled loops repeat until something happens in the loop body to change the value of loop control variable. 25 © Prepared By: Razif Razali

26 C OUNTER - CONTROLLED L OOPS Is useful when the programmer knows how many times the loop should be executed. Initialize the counter by setting it to a beginning value before entering the loop. The counter is incremented (or decremented) by the same value during each repetition. 26 © Prepared By: Razif Razali

27 E XAMPLE : R EPEAT R EQUEST AS L ONG AS R ESPONSE IN I NCORRECT Dim passWord As String = "" Do While passWord <> “KPTM" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop 27 Chapter 6 - VB 2008 by Schneider passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.

28 D O U NTIL L OOP Is executed until the condition becomes True Any Do While…. Loop can be rewritten as a Do Until….. Loop Syntax Do statement(s) Loop Until condition 28 Chapter 6 - VB 2008 by Schneider

29 E XAMPLE 1: D O L OOP U NTIL Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = “KPTM" 29 Chapter 6 - VB 2008 by Schneider

30 C OMPARING W HILE … AND U NTIL L OOPS The Do While … Loop executes while the condition is true The Do Until….. Loop executes until the condition is true Both can be used to create any type of loop 30 © Prepared By: Razif Razali

31 D O...L OOP W HILE S TATEMENT The Do...Loop While statement first executes the statements and then test the condition after each execution. The following program block illustrates the structure: Dim number As Long number = 0 Do number = number + 1 Loop While number < 201 The programs executes the statements between Do and Loop While structure in any case. Then it determines whether the counter is less than 201. If so, the program again executes the statements between Do and Loop While else exits the Loop. 31 © Prepared By: Razif Razali

32 I NFINITE L OOPS An example of an infinite loop: Dim Count As Integer Count = 1 while Count <= 25 Print Count Count = Count - 1; Wend This loop will continue executing until interrupted (Control-C) or until an underflow error occurs 32 © Prepared By: Razif Razali

33 SUMMARY A loop allows a statement or series of statements to be repeated. Do..Loop continue to execute the statements in the loop until a condition is met. Do..Loop can have the condition test at the top or the bottom of the loop and can use a While or Until to test the condition. A Do..Loop can be used to locate a selected item in a combo box. 33 © Prepared By: Razif Razali


Download ppt "CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which."

Similar presentations


Ads by Google