1 / 29

Loop and repetition

Loop and repetition. Today. Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth. Passing value to a sub procedure. Private Sub Add(num1 As Single, num2 As Single ) Call Add(x,y) The values of x and y are copied to num1 and num2.

yakov
Download Presentation

Loop and repetition

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. Loop and repetition

  2. Today • Passing values to and back from Sub procedures • Option Buttons • Do While Loops • For Loops • Population Growth

  3. Passing value to a sub procedure • Private Sub Add(num1 As Single, num2 As Single) • Call Add(x,y) • The values of x and y are copied to num1 and num2

  4. Passing value(s) back from sub procedure • As in the previous example, if the variable num1 is changed in the Add sub procedure, will the variable x be changed? • The value of the arguments will be copied back to the caller sub procedure. • This provides a tunnel for outputting several results. • Private Sub Phasor(real as single, image as single, modulus as single, phase as single) • Call Phasor(x,y,norm,angle)

  5. Complex number to its phasor form Private Sub Phasor(real as single, image as single, modulus as single, phase as single) ‘convert a complex number to its ‘phasor form modulus = sqr(real^2 + image^2) phase=atn(image/real) End Sub

  6. What is really happened in the memory • VB is not really copying back and forth. • When passing the argument to the sub prodedure, it is actually the memory block of the variable that is passed. • It is called passing by reference. (for more information, read book pp. 104-106)

  7. Option Buttons • Have value of true (clicked) or false (unclicked) • Use If statements to evaluate • Only one can be true • If button.Value Then • Tests for TRUTH (selected) • Read pp. 229-231

  8. Using Option Buttons

  9. Code If Fahrbutton.Value Then statements Elseif Celsiusbutton.Value Then statements Else statements End If

  10. Code for Program • Private Sub Convert_Click() • Outpic.Cls • 'Decare DegC and DegF as singles • Dim DegC As Single, DegF As Single • If (Fahrbutton.Value = True) Then • 'Get value for Celsius Temp • DegF = Val(inBox.Text) • 'Calculate DegF from DegC • DegC = (DegF - 32) / 1.8 • 'Output answer • Outpic.Print DegF; " degrees F equals" • Outpic.Print DegC; " degrees C. " • Else • … • … • End If • End Sub

  11. Example Start If number Equal to 1 or 0 Print The Remainder to the left of previous remains No Divide the Number By 2 Get a number Yes Is the quotient Equal to 1? No Output number Print 1 To the left of Previous remainders Yes End

  12. Convert the first decision to code IF number <> 0 OR number <> 1 THEN Do the conversion part END IF Call OutputNumber(number)

  13. Convert second decision to code • The “NO” branch includes the decision itself. IF quotient <> 1 THEN quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() IF quotient <> 1 THEN quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() IF quotient <> 1 THEN ………..

  14. Math operator \ and mod • The \ (backward slash) operator is used to divide two numbers and return an integer result. • 5 \ 2 = 2, 10 \ 3 = 3 • 5 / 2 =2.5, 10 / 3 =3.33333 • The mod operator will return the reminder of the division. • 5 mod 2 =1, 10 mod 3 =1

  15. Two solutions • Use Goto key word for unconditional jumping. • Using goto is a bad habit for programming. • Use loop structure • Do while …loop • For…Next

  16. Repetition • Along with decisions (if-then-else), repetition is the other key to making programs working • Do procedure over and over

  17. Do While Do While condition Code goes here Loop When condition becomes false, loop ends

  18. Code fragment for the example Do While quotient <> 1 quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() Loop

  19. Exponential Population Growth • Population increases by a reproductive FACTOR each year (r)

  20. Exponential Growth A bacteria divides in two at each event. How many bacteria are there after four reproductive events? 1 * 2  2 * 2  4 * 2  8* 2  16

  21. Population Growth • r = growth factor or rate (growth) • N = population (pop) • Change in population = growth rate * population • Population = Population + Change • pop = pop + growth * pop

  22. Population Growth • Two variables • pop for population • growth for growth rate • Repeat equation until pop = 1000 • pop = pop + growth * pop

  23. Do While Loop Flowchart Declare Variables Pop as SingleCounter as Integer Initialize Variables Pop = 10 Start Read growth from Text1.Text Clear Picture1 Do While Pop < 10000 T Pop = Pop + growth * pop OutputPopulation Pop < 10000 is FALSE Finish

  24. Sub Procedure Private Sub Command1_Click() Dim pop As Single, growth As Single pop = 10 growth = Val(Text1.Text) Picture1.Cls Do While pop < 10000 pop = pop + growth * pop Picture1.Print pop Loop End Sub

  25. For-Next Loops • For-Next loops are used to do an operation a specific number of times • Want to do it ten times • use a For-Next • Want to do it until number > 10000? • Use a Do While

  26. Syntax of For-Next Loop Dim counter As Integer For counter = 1 to 10 statements go here Next counter

  27. Population Growth • Instead of until population = 10000, let’s just let it run for 10 times and see what the maximum number we get is

  28. For Loop Flowchart Declare Variables Pop as SingleCounter as Integer Initialize Variables Pop = 10 Clear Picture1 Start Read growth from Text1.Text For Counter = 1 to 10 Pop = Pop + growth * pop OutputPopulation Next Counter (Counter = Counter + 1) Counter > 10 Finish

  29. Sub Procedure Private Sub Command1_Click() Dim pop As Single, growth As Single Dim counter As Integer pop = 10 growth = Val(Text1.Text) Picture1.Cls For counter = 1 To 10 pop = pop + growth * pop Picture1.Print pop Next counter End Sub

More Related