Title: Loops
1Loops
2Two types of Loop
- For.Next.Step
- Used when the number of iterations is known in
advance - Do While
- Unknown number of iterations -dependent on a
condition
3For.Next.Step
Private Sub Form_Click() Dim degC As Integer
'temperature in centigrade (whole number) Dim
degF As Single 'temperature in Farenheit (can
be a fraction) 'print headings for columns
Me.Print "deg C", "deg F" 'loop to count deg
C -12, -8, -4, 0, 4, 8,... 108, 112 For degC
-12 To 112 Step 4 'calculate equivalent in
Farenheit degF (degC (9 / 5)) 32
'print temperature in centigrate and
Farenheit Me.Print degC, degF Next degC
End Sub
4Do While
Private Sub Form_Click() Const cDayRate 4
'daytime calls cost 4p / minute Const
cEveRate 1.5 'daytime calls cost 1.5p /
minute Const cWERate 1 'daytime calls
cost 1p / minute Dim callTime As Integer
'length of call in minutes Dim chargeRate As
Integer 'charge rate 0 daytime, 1
evening, 2 weekend Dim callCost As Single
'total cost of call
5Do While
'read in length of call callTime
InputBox("Please enter length of call", "Call
Calculator") 'read in charging rate
chargeRate InputBox("Enter 1 for day, 2 for
evening, 3 for weekend", _ "Call Calculator")
'validate charging rate Do While
chargeRate lt 1 Or chargeRate gt 3 'invalid
option has been entered 'display error
message MsgBox "ERROR valid options are
1, 2 and 3 only", vbExclamation, _ "Call
Calculator" 'allow user to re-enter option
chargeRate InputBox("Enter 1 for day, 2
for evening, 3 for weekend", _ "Call
Calculator") Loop
6Do While
'cost calculation for daytime call If
chargeRate 1 Then callCost cDayRate
callTime End If 'cost calculation for
evening call If chargeRate 2 Then
callCost cEveRate callTime End If
'cost calculation for weekend call If
chargeRate 3 Then callCost cWERate
callTime End If 'display calculated cost
MsgBox "The cost of the call is " callCost
" pence", _ vbOKOnly, "Call Calculator"
End Sub
7Sorting data in an arrayBubble Sort
8The Problem
- Write a program to prompt the user to enter 10
integers and store them in an array. - Sort the integers using a pre-defined sort
algorithm called Bubble Sort. - Finally, display the sorted set of numbers in
ascending order
9Data Arrays
So far only met simple data types
Dim Number As Integer
Reserves space in memory for ONE integer number
Sometimes need to store sets of data items of the
same type
10Consider -
- We want to store all the marks for a set of
students - - We could
- Dim student1, student2, , studentn As Integer
- Not too bad if we have 10 students BUT
- What if there are 500 students?
- Much easier if we could use ONE identifier but
reserve space for 500 entries with it!
11Declaring an Array
- Dim numbers (1 To 5) As Integer
numbers(1) 200 numbers(2) 23 numbers(3)
10 numbers(4) 100 numbers(5) 2
200 23 10 100 2
12Using elements in an array
- Dim total, counter As Integer
- total numbers(1)
- For counter 2 to 5
- total total numbers(counter)
- Next counter
- OR
- total 0
- For counter 1 to 5
- total total numbers(counter)
- Next counter
13Using elements in an array
- To display all the numbers in the array together
with the total we could - MsgBox numbers in the array are _ numbers(1)
, numbers(2) , _ numbers(3) ,
numbers(4) , _ numbers(5) and the
total is total
14Displaying data
- So far only used MsgBox to display data
- BUT we can print data directly onto the form
using - Me.Print xxx
- So another way of displaying our numbers and
total would be - For counter 1 To 5
- Me.Print numbers(counter)
- Next counter
- Me.Print total
15Sorting Data in an Array
- Can now solve the first and last parts of our
problem - How do you sort data in an array?
- Known methods - called algorithms
- One algorithm for sorting is called the Bubble
Sort
16The Bubble Sort - The algorithm.
- To sort numbers x(1) to x(n) into ascending order
- For i 1 To (n-1)
- For j 1 to (n-i)
- If x(j) gt x(j1) Then
- swap x(j) and x(j1)
- Next j
- Next i
17Bubble Sort - how does it work?
- Using our 5 element array as an example
- On the first pass, content of first element
compared with second, then third, then fourth,
then fifth. In each case a swap occurs if next
number is smaller. - At end of first pass, largest number in fifth
position - On second pass, content of first element compared
with second, third and fourth. Again numbers
swapped if next one is smaller. - At end of second pass, second largest number will
be in fourth position - and so on through all 4 passes
18(No Transcript)
19Nested For Loops
Given n 5 For i 1 To (n-1) For j 1 to
(n-i) If x(j) gt x(j1) Then swap x(j)
and x(j1) Next j Next i
i j n-i x(j) x(j1)
1 1 4 200 23
1 2 4 200 10
1 3 4 200 100
1 4 4 200 2
2 1 3 23 10
2 2 3 23 100
2 3 3 100 2
3 1 2 10 23
3 2 2 23 2
4 1 1 10 2
20Swapping elements in an array
Dim temp As Integer temp numbers(j) numbers(j)
numbers(j1) numbers(j1) temp
temp variable necessary because otherwise
original value stored in numbers(j) would be lost
when numbers(j1) was assigned to numbers(j)
21Dim numbers (1 to 10) As Integer Dim i, j As
Integer loop counters Dim temp as
Integer For i 1 to 10 numbers(i)
InputBox(Enter number , Numbers) Next i For i
1 To 9 For j 1 to 10 - i If
numbers(j) gt numbers Then temp
numbers(j) numbers(j) numbers(j1)
numbers(j1) temp EndIf Next
j Next i For i 1 to 10 Me.Print
numbers(i) Next i
22Self Study/MB - This week
- Read over loop examples and run them
- Read handout from JONES on records and tables
- Read Chapter (pages 400 to 406) only from
Schneider