1 / 9

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. Do Loops. Stopping/looping condition but not sure how many iterations

Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. Do Loops • Dim counter As Integer • counter=2 • Do Whilecounter <=100 • picResults.Print counter • counter = counter +2 • Loop • Dim counter As Integer • counter=2 • Do Untilcounter >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)‏

  4. 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

  5. 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

  6. 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

  7. For Next Loops • When we know how many times we need to repeat the loop • With a consistentincrease 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

  8. 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

  9. For Next Loops • The steps don’t have to be integers • For CTR = 1 to 3 Step .5 • picResults.Print CTR • 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 • Next CTR • Design a VB program that displays in a picture box the first N multiples of an input integer

More Related