9
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

Loops

Embed Size (px)

DESCRIPTION

Loops. Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping. READ LOOP: WRITE SUB decrement JPOSLOOP HALT decrement :1. Do Loops. Stopping/looping condition but not sure how many iterations - PowerPoint PPT Presentation

Citation preview

Page 1: Loops

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

Page 2: Loops

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

picResults.Print InputNum InputNum=inputbox(“Enter a number to print, 0 to stop”)

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”)

Loop

Print even numbers between 2 and 100 in a picturebox

Page 3: Loops

Do Loops Dim counter As Integer counter=2 Do While counter <=100

picResults.Print counter counter = counter +2

Loop

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) Opposite to one another

Same action (Loop body: Do … Loop)

Page 4: Loops

Do Loops

Program to compute the average for any class exam at CSBSJUEnter any number of gradesWhen the flag (-1) is provided

end of input and print average

Algorithm + programShow program

Page 5: Loops

Do Loops

Program to get the average of any number of grades (enter -1 to exit)Sum = 0, count=0, average=0grade=input by userWhile grade <> -1

Add grade to sumAdd 1 to countEnter another grade

Divide sum by count to obtain the averagePrint average

Page 6: Loops

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

Page 7: Loops

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

Page 8: Loops

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 CTRNext CTR

Can be positive or negative (display even numbers from 100 to 2)For CTR = 100 to 2 Step -2

picResults.Print CTRNext CTR

Page 9: Loops

For Next Loops The steps don’t have to be integers

For CTR = 1 to 3 Step .5picResults.Print CTR

Next CTR Suppose we want to display all even numbers

between 2 and another number specified by the userDim CTR as Integer, N As IntegerN = txtEndBox.TextFor CTR = 2 to N Step 2

picResults.Print CTRNext CTR

Design a VB program that displays in a picture box the first N multiples of an input integer