loop and repetition

29
Loop and repetition

Upload: yakov

Post on 19-Jan-2016

40 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Loop and repetition

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

Page 3: Loop and repetition

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

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

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

Page 6: Loop and repetition

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

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

Using Option Buttons

Page 9: Loop and repetition

Code

If Fahrbutton.Value Then

statements

Elseif Celsiusbutton.Value Then

statements

Else

statements

End If

Page 10: Loop and repetition

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

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

Convert the first decision to code

IF number <> 0 OR number <> 1 THEN

Do the conversion partEND IF

Call OutputNumber(number)

Page 13: Loop and repetition

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

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

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

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

Do While

Do While condition

Code goes here

Loop

When condition becomes false, loop ends

Page 18: Loop and repetition

Code fragment for the example

Do While quotient <> 1

quotient = number \ 2

reminder = number mod 2

number = quotient

call PrintReminder()

Loop

Page 19: Loop and repetition

Exponential Population Growth

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

Page 20: Loop and repetition

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

Page 21: Loop and repetition

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

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

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

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

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

Syntax of For-Next Loop

Dim counter As Integer

For counter = 1 to 10

statements go here

Next counter

Page 27: Loop and repetition

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

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

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