Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 - Visual Basic Schneider

Similar presentations


Presentation on theme: "Chapter 6 - Visual Basic Schneider"— Presentation transcript:

1 Chapter 6 - Visual Basic Schneider
Repetition Chapter 6 - Visual Basic Schneider

2 Chapter 6 - Visual Basic Schneider
Outline & Objectives Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops Chapter 6 - Visual Basic Schneider

3 Types of LOOP Structures
Do While ……. Loop Do Until …… Loop For …… Next loop Chapter 6 - Visual Basic Schneider

4 Basic Definition Looping: the process of repeating a series of statements multiple times until a criteria is met Chapter 6 - Visual Basic Schneider

5 Basic Components of Loops
Loop control variable: A variable used to determine whether a loop will be executed Loop body: The statement (s) that are executed each time a loop repeats Chapter 6 - Visual Basic Schneider

6 The Do While ……. Loop Do While condition is true statement(s) Loop
The statements between Do and Loop do not have to be indented. But indenting readability of the program As soon n as you see the word Do, your eyes can easily scan down the program to find the matching Loop statement. Chapter 6 - Visual Basic Schneider

7 Flowchart for a Do While Loop
Is the condition true No Yes Execute statements within the loop Execute statements that follow the loop Chapter 6 - Visual Basic Schneider

8 Example (Displays the numbers from 1 through 10)
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 The word While proclaims testing at the top. Output: Chapter 6 - Visual Basic Schneider

9 The Do While ……. Loop Is executed as long as the condition is True.
If condition is False then the next statement after the Loop is executed. Chapter 6 - Visual Basic Schneider

10 Controlling Loops 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. Chapter 6 - Visual Basic Schneider

11 Example of event-controlled loops
passWord = "" Do While passWord <> “ADMIN" passWord = UCase(InputBox("What is the password?")) Loop Chapter 6 - Visual Basic Schneider

12 Counter-controlled Loops
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. Chapter 6 - Visual Basic Schneider

13 Example for Counter-controlled loop
num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Output: Chapter 6 - Visual Basic Schneider

14 Example

15 Example Dim x As Integer Dim s As Integer Do While x >= 2 x = x + 1
s = s + x Loop picOutput.Print x; s

16 Example

17 Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x
Loop While x >= 2 picOutput.Print x; s

18 Chapter 6 - Visual Basic Schneider
Do Until ……. Loop Is executed until the condition becomes True Any Do While…. Loop can be rewritten as a Do Until ….. Loop Chapter 6 - Visual Basic Schneider

19 Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x
Loop Until x >= 2 picOutput.Print x; s

20 Example Dim x As Integer Dim s As Integer Do x = x + 1 s = s + x
Loop Until x <= 2 picOutput.Print x; s

21 Example Dim x As Integer Dim s As Integer Do Until x >= 2 x = x + 1
s = s + x Loop picOutput.Print x; s

22 Comparing While… and Until Loops
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 Chapter 6 - Visual Basic Schneider

23 Converting While loop to Until loop
Do While Condition Action(s) Loop Do Until Not(Condition) Action(s) Loop Equivalents to Do While x <= y Action(s) Loop Do Until x > y Action(s) Loop Equivalents to Do While x <=3 and x >=10 Action(s) Loop Do Until Not(x<=3 and x>=10) Action(s) Loop Equivalents to

24 Review How many times will the following loops execute? Infinite loop
num = 11 Do While num <= 10 picOutput.Print num; num = num + 1 Loop num = 11 Do picOutput.Print num; num = num + 1 Loop until num <= 10 Infinite loop Chapter 6 - Visual Basic Schneider 24

25 Review Which loop is infinite? i = 0 Do i = i + 1 Loop While i < 10
i = 1 Do While i < 10 i = i + 1 Loop NO NO i = 0 Do i = i + 10 Loop Until i < 10 i = 11 Do Until i < 10 Loop Yes Yes

26 EOF Function EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False Chapter 6 - Visual Basic Schneider

27 will be true if the end of file has been reached, and false otherwise
Example will be true if the end of file has been reached, and false otherwise Dim count As Integer Open “DATA.TXT” For Input As #1 Do While NOT EOF(1) Input #1, x count= count + 1 s = s + x Loop Print count;s;x The loop will repeated as long as the end of file is not reached 27 27

28 Counters and Accumulators
A counter is a numeric variable that keeps track of the number of items that have been processed in a loop. An accumulator is a numeric variable that holds a sub-total during multiple passes through a loop. Chapter 6 - Visual Basic Schneider

29 Example: Counter & Accumulator
Private Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single, value As Single Open "COINS.TXT" For Input As #1 numCoins = 0 sum = 0 Do While Not EOF(1) Input #1, value numCoins = numCoins + 1 sum = sum + value Loop picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents." End Sub COINS.TXT 50 10 5 25 Chapter 6 - Visual Basic Schneider

30 Compare Do While ……. Loop Do ……. Loop While Do ……. Loop Until
Do Until ……. Loop Chapter 6 - Visual Basic Schneider

31 For … Next Loop 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 Chapter 6 - Visual Basic Schneider

32 Example 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 Control variable Terminating value Initial Value Chapter 6 - Visual Basic Schneider

33 picOutput.Print numVar; Next numVar Output: 1 3 5
Example Dim numVar As Integer For numVar = 1 To 5 Step 2 picOutput.Print numVar; Next numVar Output: Chapter 6 - Visual Basic Schneider

34 When a For 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. Chapter 6 - Visual Basic Schneider

35 Rules for Using For ... Next loop
You should never modify the value of the loop control variable in the loop body. Each For loop must end with a Next statement. Chapter 6 - Visual Basic Schneider

36 Example Private Sub cmdDisplay_Click() Dim i As Integer
For i = 1 To 10 picOutput.Print "*"; Next i End Sub Output: ********** Chapter 6 - Visual Basic Schneider

37 Example Private Sub cmdDisplay_Click()
Dim i As Integer, stars As Integer stars = Val(InputBox("Row length (1-20) :")) For i = 1 To stars picOutput.Print "*"; Next i End Sub Chapter 6 - Visual Basic Schneider

38 Example Dim numVar As Integer For numVar = 8 To 1 Step -2
picOutput.Print numVar; Next numVar Output: Displays Chapter 6 - Visual Basic Schneider

39 which of the following statements is true about the following code
For i = 1 To 5.8 Step 1 pic1.Print i; Next I This code will print numbers from 1 to 6 This code will print numbers from 1 to 5 Run-time error This code will print numbers from 1 to 5.8

40 What is the output of the following code?
Dim i As Integer For i = 9 To 7 Print i; Next i Nothing will be printed

41 What is the output of the following code?
Dim i as integer i=4 for i=3 To 5 print 2*i; Next i print i

42 Example Dim x as integer x = 0 for x=3 To 1 print x; Next x print x 3

43 How many stars (*) are printed when the following code is executed?
Dim i as Integer for i = 1 To 6 Step 1 print "*" i=i+2 next i 2 stars

44

45 Private Sub Command1_Click()
For x = 1 To 3 Step 0 Print x; Next Print " finally "; x End Sub Infinite loop

46 Nested Loops For outer = 1 To 4 For inner = 1 To 2 .. Next inner
Next outer Use indentation to improve readability In this example overtime the outer loop is executed once, the inner loop is twice Each loop must have a unique loop control variable. One loop is entirely contained within the outer loop Chapter 6 - Visual Basic Schneider

47 Example: Display a 3x3 rectangle of stars
Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 3 For j = 1 To 3 picOutput.Print "*"; Next j picOutput.Print Next i End Sub *** Display a 3 by 3 square of stars Chapter 6 - Visual Basic Schneider

48 Review 2 5 7 For i= 1 To 5 Step 2 i=i+1 picOutput.Print i; Next i -5
For i= -5 To -1 Step - 2 picOutput.Print i; Next i 2 5 7 For i= 1 To 5 Step 2 i=i+1 picOutput.Print i; Next i -5 Chapter 6 - Visual Basic Schneider

49 Review *** * ** ** * *** Private Sub cmdDisplay_Click()
Dim i As Integer, j As Integer For i = 1 To 3 For j = 1 To i picOutput.Print "*"; Next j picOutput.Print Next i End Sub Private Sub cmdDisplay_Click() Dim i As Integer, j As Integer For i = 1 To 3 For j = i To 3 picOutput.Print "*"; Next j picOutput.Print Next i End Sub Display a 10by 10 square of stars Chapter 6 - Visual Basic Schneider

50 How many times the statement print "Good Luck" will be executed
Dim i as integer, j as integer for i=0 To 3 for j=0 To 0 print "Good Luck" Next j Next i 4


Download ppt "Chapter 6 - Visual Basic Schneider"

Similar presentations


Ads by Google