loop and repetition
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
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
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
Using Option Buttons
Code
If Fahrbutton.Value Then
statements
Elseif Celsiusbutton.Value Then
statements
Else
statements
End If
Code for ProgramPrivate Sub Convert_Click()Outpic.Cls'Decare DegC and DegF as singlesDim 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 IfEnd Sub
ExampleStart
Get a number
Divide the Number
By 2
Is the quotient Equal to 1?
PrintThe Remainder
to the left of previous remains
No
Print 1 To the left of
Previous remaindersEnd
YesOutput number
If numberEqual to 1 or
0
Yes
No
Convert the first decision to code
IF number <> 0 OR number <> 1 THEN
Do the conversion partEND IF
Call OutputNumber(number)
Convert second decision to code
• The “NO” branch includes the decision itself.IF quotient <> 1 THEN
quotient = number \ 2reminder = number mod 2number = quotientcall PrintReminder()IF quotient <> 1 THEN
quotient = number \ 2 reminder = number mod 2number = 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
StartDeclare Variables
Pop as SingleCounter as Integer
Do WhilePop < 10000
Finish
Initialize VariablesPop = 10
Read growth from Text1.Text
Clear Picture1
Pop = Pop + growth * pop
OutputPopulation
Pop < 10000 is FALSE
T
Sub Procedure
Private Sub Command1_Click()Dim pop As Single, growth As Singlepop = 10growth = Val(Text1.Text)Picture1.ClsDo While pop < 10000 pop = pop + growth * pop Picture1.Print popLoopEnd 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
StartDeclare Variables
Pop as SingleCounter as Integer
ForCounter = 1
to 10
Finish
Initialize VariablesPop = 10
Read growth from Text1.Text
Clear Picture1
Pop = Pop + growth * pop
OutputPopulation
Counter > 10Next Counter
(Counter =Counter + 1)
Sub Procedure
Private Sub Command1_Click()Dim pop As Single, growth As SingleDim counter As Integerpop = 10growth = Val(Text1.Text)Picture1.ClsFor counter = 1 To 10 pop = pop + growth * pop Picture1.Print popNext counterEnd Sub