290 likes | 389 Views
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.
E N D
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
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)
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
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)
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
Code If Fahrbutton.Value Then statements Elseif Celsiusbutton.Value Then statements Else statements End If
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
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
Convert the first decision to code IF number <> 0 OR number <> 1 THEN Do the conversion part END IF Call OutputNumber(number)
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 ………..
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
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
Repetition • Along with decisions (if-then-else), repetition is the other key to making programs working • Do procedure over and over
Do While Do While condition Code goes here Loop When condition becomes false, loop ends
Code fragment for the example Do While quotient <> 1 quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() Loop
Exponential Population Growth • Population increases by a reproductive FACTOR each year (r)
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
Population Growth • r = growth factor or rate (growth) • N = population (pop) • Change in population = growth rate * population • Population = Population + Change • pop = pop + growth * pop
Population Growth • Two variables • pop for population • growth for growth rate • Repeat equation until pop = 1000 • pop = pop + growth * pop
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
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
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
Syntax of For-Next Loop Dim counter As Integer For counter = 1 to 10 statements go here Next counter
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
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
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