Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While...

29
Loop and repetition

description

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

Transcript of Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While...

Page 1: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Loop and repetition

Page 2: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Today

• Passing values to and back from Sub procedures

• Option Buttons• Do While Loops• For Loops• Population Growth

Page 3: 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

Page 4: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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)

Page 5: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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 formmodulus = sqr(real^2 + image^2)phase=atn(image/real)

End Sub

Page 6: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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)

Page 7: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 8: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Using Option Buttons

Page 9: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Code

If Fahrbutton.Value Thenstatements

Elseif Celsiusbutton.Value Then statementsElse

statementsEnd If

Page 10: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 11: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 12: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Convert the first decision to code

IF number <> 0 OR number <> 1 THEN

Do the conversion partEND IFCall OutputNumber(number)

Page 13: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

………..

Page 14: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 15: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 16: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Repetition

• Along with decisions (if-then-else), repetition is the other key to making programs working

• Do procedure over and over

Page 17: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Do While

Do While conditionCode goes here

Loop

When condition becomes false, loop ends

Page 18: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Code fragment for the example

Do While quotient <> 1quotient = number \ 2reminder = number mod 2number = quotientcall PrintReminder()

Loop

Page 19: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Exponential Population Growth

• Population increases by a reproductive FACTOR each year (r)

Page 20: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Exponential GrowthA 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

Page 21: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Population Growth

• r = growth factor or rate (growth)• N = population (pop)• Change in population = growth rate *

population• Population = Population + Change• pop = pop + growth * pop

Page 22: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Population Growth

• Two variables– pop for population– growth for growth rate

• Repeat equation until pop = 1000– pop = pop + growth * pop

Page 23: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 24: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 25: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 26: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Syntax of For-Next Loop

Dim counter As IntegerFor counter = 1 to 10 statements go hereNext counter

Page 27: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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

Page 28: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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)

Page 29: Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

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