lesson 2.3 looping
TRANSCRIPT
-
8/19/2019 Lesson 2.3 Looping
1/36
CONTROL STATEMENTS AND FUNCTIONS
Unit 2
-
8/19/2019 Lesson 2.3 Looping
2/36
LOOPING
Lesson 2.3
-
8/19/2019 Lesson 2.3 Looping
3/36
Objectives:
At the end of the lesson, you should be able to:
1. Create Loops usin !or"#e$t %tate&ents and
2. Control the nu&ber of iterations in a loop
usin %tep.
-
8/19/2019 Lesson 2.3 Looping
4/36
Loops
At instances 'hen 'e 'ant the co&puter to doa tas( repeatedly for a certain nu&ber of ti&es,loopin beco&es necessary. #ote that this is not
the sa&e as havin a user re"enter a value until a valid value is iven )li(e our %* prora& inLesson 2.2+. n such a case it is the user doin therepeated tas(, not the co&puter. -he loop 'asfirst conceptualied by Ada /yron )recall Lesson1.1+.
-
8/19/2019 Lesson 2.3 Looping
5/36
-he !or"#e$t Loop
One 0/ state&ent that allo's loopin is calledthe !or"#e$t state&ent or loop.
!or variable start value -o end value %tep incre&ent%tate&ents
#e$t
-
8/19/2019 Lesson 2.3 Looping
6/36
-he !or"#e$t Loop
-his loop repeats a set of state&ents a certainnu&ber of ti&es. -hat nu&ber is deter&ined bythree values"the start value, the end value, and
the incre&ent. #ote that the state&ents to berepeated are enclosed by !or and #e$t. #e$t isthe end &ar(er of the loop just as nd %ub is theend &ar(er for a subroutine. -he variable used ina !or"#e$t state&ent &ay be used in thestate&ents bet'een !or and #e$t.
Continuation
-
8/19/2019 Lesson 2.3 Looping
7/36
-he !or"#e$t Loop
n order to understand this state&ent better, 'e shall conduct a little e$peri&ent.
Continuation
-
8/19/2019 Lesson 2.3 Looping
8/36
-ry this on your co&puter
-
8/19/2019 Lesson 2.3 Looping
9/36
-ry this on your co&puter
#a&e the te$t bo$es )fro& top to botto&+ ast$t%tart, t$tnd and t$tncr. -hen, na&e, too,the co&&and button as c&dO( and the list bo$
on the riht as lstLoop. 4eclare $, start5val,end5val, incr as variables of type %inle. !inally,assin the follo'in codes to c&dO(.
Continuation
-
8/19/2019 Lesson 2.3 Looping
10/36
-ry this on your co&puter
6rivate %ub c&dO(5Clic( ) +
lstLoop.Clear
start5val 0al )t$t%tart.-e$t+
end5val 0al )t$tnd.-e$t+
incr 0al )t$tncr.-e$t+
!or $ start5val to end5val %tep incr
lstLoop.Addte& )789+
#e$t
nd %ub
Continuation
-
8/19/2019 Lesson 2.3 Looping
11/36
-ry this on your co&puter
%ave the prora& then, run it. -a(e note of thenu&ber of 8 that appears as you chane the values in the te$t bo$es.
Continuation
-
8/19/2019 Lesson 2.3 Looping
12/36
-he !or"#e$t Loop
#ote that the object 'hich loo(s li(e a huete$t bo$ is actually a list bo$.
A list bo$ is different fro& a te$t bo$, for you
can add ite&s to it via the state&entList/o$5na&e. Addte& )ite&s to be added+. nour prora&, 'e shall add 8 to the list bo$. -his
'ill be repeated several ti&es dependin on the values 'e enter in the te$t bo$es. -he state&ent,1stLoop.Clear, is used to clear the contents of thelist bo$ prior to loadin it 'ith 8;s.
Continuation
-
8/19/2019 Lesson 2.3 Looping
13/36
-he !or"#e$t Loop
n the course of runnin the prora&, you &ayhave co&e across the follo'in outputs.
Continuation
ForStatement
Number of
*’s
For x = 1To 10Step 1
10
For x = 1To 6 Step2
3
For x = 3To 5 Step
1
-
8/19/2019 Lesson 2.3 Looping
14/36
-he !or"#e$t Loop
f you;ve tried enterin a ero incre&ent, you&ay have noticed soðin unusual < 0/haned=
Continuation
Warning! A ero incre&ent 'ill result in an infinite loop < a
loop that never ter&inates.
>hy this happens 'ill beco&e evident once 'e understand ho' the values affect the nu&berof iterations or loops.
-
8/19/2019 Lesson 2.3 Looping
15/36
-he !or"#e$t Loop
/elo' is a flo'chart that illustrates 'hat ta(esplace in our !or"#e$t loop.
Continuation
$ start5val
add an 8 to 1stLoop
$ $ ? incr
$ @ end5val
end
es#o
-
8/19/2019 Lesson 2.3 Looping
16/36
-he !or"#e$t Loop
>hat happens at first is that our du&&y variable $ ta(es on the value of start5val. t )$+ isthen chec(ed if it is reater than end5val. >hile
it )$+ is not reater than end5val, t'o thinshappen: An 8 is added to the list bo$ and incr isadded to the value of $ via the state&ent $ $ ?incr.
Continuation
-
8/19/2019 Lesson 2.3 Looping
17/36
-he !or"#e$t Loop
!or e$a&ple, consider !or $ 1 -o B %tep 3.nitially $ is assined the value of 1. %ince it is not yet reater than B, an iteration is &ade and 3 is
added to it &a(in it . %ince it is not yet reaterthan B, another iteration is &ade and 3 is addedto it, &a(in it D. #o', that it is reater than B,the iterations cease. A total of t'o iterations 'ere&ade thus, 'e see t'o 8;s
Continuation
-
8/19/2019 Lesson 2.3 Looping
18/36
-he !or"#e$t Loop
>hen the incre&ent is ero, $ 'ill never reachor surpass the end value hence, 'e have aninfinite loop.
Continuation
-
8/19/2019 Lesson 2.3 Looping
19/36
Usin the 4u&&y 0ariable
#o' that 'e understand ho' !or"#e$t 'or(s,there are other thins 'e should learn about it.One is that !or $ 1 -o 1E %tep 1 is eFuivalent to
!or $ 1 -o 1E. >hat it &eans is that if theincre&ent is 1, 'e do not have to add %tep 1. -heother is that the du&&y variable can be used.
-
8/19/2019 Lesson 2.3 Looping
20/36
-ry this on your co&puter
n the Loop -ester prora&, chane thestate&ent 1stLoop.Addte& )789+ to1stLoop.Addte& )$+ and run the prora&,
ta(in note of the outputs as you chane the values in the te$t bo$es.
We can now see the values x holds in a loop. Keep this in mind as we tackle another program.
-
8/19/2019 Lesson 2.3 Looping
21/36
-
8/19/2019 Lesson 2.3 Looping
22/36
-he 6ri&e valuator
!or e$a&ple, to test if H is pri&e, 'e need to testthe nu&bers 2 to B aainst it. Of course, 'e shallfind that H is perfectly divisible by 2 and 3 hence,
H is not pri&e. -he Fuestion is this: Io' do 'echec( for divisibilityG -he ans'er lies in t'o&athe&atical operators < division )J+ andinteer division )K+.
Continuation
-
8/19/2019 Lesson 2.3 Looping
23/36
-he 6ri&e valuator
#ote that H is divisible by 3 because HJ3 is eFualto HK3 )since HJ3 and HK3 are both eFual to 2+.On the other hand, H is not divisible by since
HJ is not eFual to HK )since HJ 1.B and HK 1+. -hus, 'e can say that # is divisible by if andonly if, #J #K. #o', all 'e need is for to vary fro& 2 to #"1.
Continuation
-
8/19/2019 Lesson 2.3 Looping
24/36
-he 6ri&e valuator
Let us assu&e that 'e have a for& 'ith aco&&and button c&dO(, a te$t bo$ t$t#, and alabel lblMesult. >hat should happen is that the
user supplies a nu&ber via t$t#, clic(s c&dO(,and vie's the result of 'hether the nu&ber 'aspri&e or not in lblMesult.
Continuation
-
8/19/2019 Lesson 2.3 Looping
25/36
-he 6ri&e valuator
>e also need t'o variables: # to held thenu&ber supplied by the user and to hold thenu&bers to be tested aainst # for divisibility.
Continuation
n code,4i& #, As nteer
6rivate %ub c&dO(5Clic() +# 0al )t$t#.-e$t+
nd %ub
-
8/19/2019 Lesson 2.3 Looping
26/36
-he 6ri&e valuator
#o', 'e need to test if perfectly divides #, because if it does, # is not pri&e.
Continuation
n code,
4i& #, As nteer
6rivate %ub c&dO(5Clic() +# 0al )t$t#.-e$t+
f #J #K -henlblMesult.Caption 7#ot 6ri&e9nd f
nd %ub
-
8/19/2019 Lesson 2.3 Looping
27/36
-he 6ri&e valuator
#otice that for this to 'or(, &ust vary in valuefro& 2 to #"1. -his is 'here the !or"#e$t loop co&esin.
Continuation
n code,
4i& #, As nteer
6rivate %ub c&dO(5Clic() +# 0al )t$t#.-e$t+!or 2 -o #"1
f #J #K -henlblMesult.Caption 7#ot 6ri&e9
nd f #e$t
nd %ub
-
8/19/2019 Lesson 2.3 Looping
28/36
-he 6ri&e valuator
n hindsiht, it see&s a 'aste of co&putinenery to test all ;s 'hen 'e have alreadyencountered an 'hich perfectly divides #.
!or e$a&ple, the co&puter has found that 2perfectly divides H, so it does not need to chec( if
3, , or B also perfectly divides H. >e need to finda 'ay to step out of the loop once this happens.!ortunately, there is a state&ent that allo's that.
Continuation
-
8/19/2019 Lesson 2.3 Looping
29/36
-he 6ri&e valuatorContinuation
4i& #, As nteer
6rivate %ub c&dO(5Clic() +
# 0al )t$t#.-e$t+
!or 2 -o #"1
f #J #K -hen
lblMesult.Caption 7#ot 6ri&e9
$it !ornd f
#e$t
nd %ub
-
8/19/2019 Lesson 2.3 Looping
30/36
-ry this on your co&puter
Construct the 6ri&e valuator prora& 'e;ve just discussed. 0erify if it 'or(s.
-
8/19/2019 Lesson 2.3 Looping
31/36
-he 6ri&e valuatorContinuation
ou probably noticed that the prora& does 'or( 'hen # is not pri&e. >hen # is pri&e,ho'ever, it doesn;t say so. 'ill leave it up to you
to e$peri&ent 'here in the code this line should be placed.
lblMesult.Caption 76ri&e=9
-
8/19/2019 Lesson 2.3 Looping
32/36
#estin Loops
n the previous lesson, 'e found that 'e can nestf state&ents. >e can do the sa&e for !or"#e$t.-he rules of nestin still apply, of course.
C i i
-
8/19/2019 Lesson 2.3 Looping
33/36
#estin Loops
Iere is an e$a&ple:!or A 1 -o B
!or / 1 to 1E
)%tate&ents+#e$t
#e$t
Continuation
In this example, for every loop of A, B has made tenloops. Since A will loop five times, the statementsounded y the B loop would have een repeated fifty times!
-
8/19/2019 Lesson 2.3 Looping
34/36
Other Loops
Aside fro& !or"#e$t, there are other loopinstate&ents that can be used. -he three sets ofcode outlined belo' are essentially the sa&e.
!or * B to 1E %tep 2
)Mepeated %tate&ents+
#e$t
C ti ti
-
8/19/2019 Lesson 2.3 Looping
35/36
Other Loops
* B >hile * N 1E
)Mepeated %tate&ents+
* * ? 2 >end
Continuation
C ti ti
-
8/19/2019 Lesson 2.3 Looping
36/36
Other Loops
* B4o >hile * N 1E
)Mepeated %tate&ents+
* * ? 2Loop
Continuation
"he last two sets of code are more literaltranslations of the flowchart that was shownearlier.