Top Banner
Repetition Chapter 6 - Visual Basic Schneider 1
35

Repetition Chapter 6 - Visual Basic Schneider 1 Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops Chapter 6 -

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Repetition

Chapter 6 - Visual Basic Schneider 1

Page 2: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops

Chapter 6 - Visual Basic Schneider 2

Page 3: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Do While ……. Loop Do Until …… Loop For …… Next loop

Chapter 6 - Visual Basic Schneider 3

Page 4: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Looping: the process of repeating a series of statements multiple times until a criteria is met

Chapter 6 - Visual Basic Schneider 4

Page 5: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 5

Page 6: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Do While condition is true statement(s) Loop

Chapter 6 - Visual Basic Schneider 6

Page 7: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Chapter 6 - Visual Basic Schneider 7

Is the condition true

Execute statementswithin

the loop

Execute statementsthat follow the loop

Yes

No

Page 8: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 LoopEnd Sub

Chapter 6 - Visual Basic Schneider 8

Page 9: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 9

Page 10: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 10

Page 11: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

passWord = ""Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the

password?"))Loop

Chapter 6 - Visual Basic Schneider 11

Page 12: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 12

Page 13: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

num = 1Do While num <= 10 picOutput.Print num; num = num + 1Loop

Chapter 6 - Visual Basic Schneider 13

Page 14: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Is executed until the condition becomes True

Any Do While…. Loop can be rewritten as a Do Until ….. Loop

Chapter 6 - Visual Basic Schneider 14

Page 15: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Private Sub cmdDisplay_Click() Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then Do passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM" End If Open txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1End Sub

Chapter 6 - Visual Basic Schneider 15

Page 16: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Private Sub cmdEstimate_Click() Dim amt As Single, yrs As Integer ' Years to deplete savings account picResult.Cls amt = 15000 yrs = 0 Do amt = amt * 1.05 - 1000 yrs = yrs + 1 Loop Until amt <= 0 picResult.Print "It takes"; yrs; "years to deplete the account."End Sub

Chapter 6 - Visual Basic Schneider 16

Page 17: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 17

Page 18: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

EOF(n) is True if the end of the file having reference number n has been reached. Otherwise, it is False

Example: Open “PHONE.TXT” for Input As #1Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNumLoopClose #1

Chapter 6 - Visual Basic Schneider 18

Page 19: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 19

Page 20: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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

Chapter 6 - Visual Basic Schneider 20

Page 21: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Do While ……. Loop Do ……. Loop While Do ……. Loop Until Do Until ……. Loop

Chapter 6 - Visual Basic Schneider 21

Page 22: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

num = 11Do While num <= 10 picOutput.Print num; num = num + 1Loop

Chapter 6 - Visual Basic Schneider 22

num = 11Do picOutput.Print

num; num = num + 1Loop until num

<= 10

How many times will the following loops execute?

Page 23: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Do While i < 10 i = i + 1

Loop

Chapter 6 - Visual Basic Schneider 23

Do i = i + 1

Loop While i < 10

Which loop is infinite?

Do Until i < 10Loop

Do i = i + 10 Loop Until i < 10

Page 24: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 24

Page 25: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 ^ 2Next i

End Sub

Chapter 6 - Visual Basic Schneider 25

Initial Value

Control variable

Terminating value

Page 26: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Dim numVar As IntegerFor numVar = 1 To 5 Step 2 picOutput.Print numVar; Next numVar

Output: 1 3 5

Chapter 6 - Visual Basic Schneider 26

Page 27: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

1. The control variable is assigned the initial value.

2. 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.)

3. Iteration continues until the terminating value is exceeded.

Chapter 6 - Visual Basic Schneider 27

Page 28: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 28

Page 29: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Private Sub cmdDisplay_Click() Dim i As Integer For i = 1 To 10 picOutput.Print "*"; Next iEnd Sub

Output: **********

Chapter 6 - Visual Basic Schneider 29

Page 30: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 iEnd Sub

Chapter 6 - Visual Basic Schneider 30

Page 31: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Dim numVar As IntegerFor numVar = 8 To 1 Step -2 picOutput.Print numVar;Next numVar

Output: 8 6 4 2

Chapter 6 - Visual Basic Schneider 31

Page 32: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

For outer = 1 To 4 For inner = 1 To 2 .. .. Next innerNext outer

Chapter 6 - Visual Basic Schneider 32

Page 33: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

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 iEnd Sub

Chapter 6 - Visual Basic Schneider 33

Page 34: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

For i= -5 To -1 Step - 2 picOutput.Print i; Next ipicOutput.Print i;

Chapter 6 - Visual Basic Schneider 34

For i= 1 To 5 Step 2 i=i+1

picOutput.Print i;

Next ipicOutput.Print i;

Page 35: Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -

Private Sub cmdDisplay_Click()

Dim i As Integer, j As Integer

For i = 1 To 10 For j = i To 10 picOutput.Print "*"; Next j picOutput.Print Next iEnd Sub

Chapter 6 - Visual Basic Schneider 35

Private Sub cmdDisplay_Click()

Dim i As Integer, j As Integer

For i = 1 To 10 For j = 1 To i picOutput.Print

"*"; Next j picOutput.Print Next iEnd Sub