Transcript VB3 Slides

Loops

Counting down from N to 1 in assembly we used JUMP
statements to repeat previous instructions thus looping
READ
LOOP:
WRITE
SUB decrement
JPOS
LOOP
HALT
decrement :1
Do Loops

Stopping/looping condition but not sure how many iterations


good with flags (or sentinels)
Come in two forms

Do While --- LOOPING CONDITION



Dim InputNum As Single
InputNum=inputbox(“Enter a number to print, 0 to stop”)
Do While InputNum<>0





Loop
What would happen if I don’t read input again from user inside loop?
Do Until --- EXIT CONDIITON



Dim InputNum As Single
InputNum=inputbox(“Enter a number to print, 0 to stop”)
Do Until InputNum=0




picResults.Print InputNum
InputNum=inputbox(“Enter a number to print, 0 to stop”)
picResults.Print InputNum
InputNum=inputbox(“Enter a number to print, 0 to stop”)
Loop
Print even numbers between 2 and 100 in a picturebox
Do Loops










Dim counter As Integer
counter=2
Do Until counter >100
 picResults.Print counter
 counter = counter +2
Loop
Same initialization phase for loop variable (Outside loop)
Syntactically different exit conditions (Semantically the same)


Dim counter As Integer
counter=2
Do While counter <=100
 picResults.Print counter
 counter = counter +2
Loop
Opposite to one another
Same action (Loop body: Do … Loop)
Do Loops

Program to compute the average for any class
exam at CSBSJU


Enter any number of grades
When the flag (-1) is provided


end of input and print average
Algorithm + program

Show program
Do Loops

Program to get the average of any number of
grades (enter -1 to exit)



Sum = 0, count=0, average=0
grade=input by user
While grade <> -1





Add grade to sum
Add 1 to count
Enter another grade
Divide sum by count to obtain the average
Print average
Do Loops


Private Sub cmdAverageButton_Click()

‘program to compute the average exam score

Dim count As Integer

Dim grade As Single, sum As Single, avg As Single

grade = InputBox (“Enter the first exam score:”, ”Exam
Scores”)

Do While grade <> -1
 sum = sum + grade
 count = count + 1
 grade = InputBox(“Enter another score Type -1 to end.”,
“Exam Scores”)

Loop

avg = sum/count

MsgBox “The average is: ” & FormatNumber(avg)
End Sub
For Next Loops

When we know how many times we need to repeat the loop


With a consistent increase or decrease  For Next Loops are better
Display values between 1 to 5 (vs. until user inputs -1)


Dim CTR As Integer
For CTR = 1 to 5







picResults.Print CTR
Next CTR
After every loop, the loop counter is incremented by 1 (default)
Initialization: CTR=1
Exit Condition: CTR>5
Action: Results.Print CTR
Combines the initialization and exit conditions into one line

Previously initialization was done before loop
For Next Loops

After every loop, the loop counter is incremented by 1
(default)

Can be changed by specifying steps (display even
numbers from 2 to 100)

For CTR = 2 to 100 Step 2



picResults.Print CTR
Next CTR
Can be positive or negative (display even numbers from
100 to 2)

For CTR = 100 to 2 Step -2


picResults.Print CTR
Next CTR
For Next Loops

The steps don’t have to be integers

For CTR = 1 to 3 Step .5



Next CTR
Suppose we want to display all even numbers
between 2 and another number specified by the user



Dim CTR as Integer, N As Integer
N = txtEndBox.Text
For CTR = 2 to N Step 2



picResults.Print CTR
picResults.Print CTR
Next CTR
Design a VB program that displays in a picture
box the first N multiples of an input integer