infomation technology HW

profileDre_fingerz
ch_15_0001.pdf

\ow consider the possibility that a customer may have more than one book for Ben to sign' lt's

e'en possibie ttrut u ..rrlo*ir, not knowing aboui the book signing in advance' has left his or her

'rook at home and is standing in line for the sole purpose oi-"ttittg Ben' W'hat changes wili

need to be mad.e to the soiut[n shown in Figure ri-ri ffte current instructions within the loop

siill must be repeated for each customer. tn iiaitlo,, however' all but the last instruction in the

loop (the laawQ" lhe ct t-stctwcel' instruction) must be repeated for each of the customer's books'

Youwillneedtouseanestedlooptoincludethisadditionaltaskinthesolution.Benwillthank --lre customer only after ail of the customer's books

have been signed' so the tl'rar'tk lhe cvtsLow'er

instruction should not be part of the nested loop'

Iigure 15-2 shows the modified solution, which contains an outer loop and a nested loop' The

rluter loop begins with repent wlnLle there Are cvtslovwers Lw lLwe: and it ends with the last ewd

,::o,at. Thenested ,d;|ilt";# ;r;. i; r ead,t of t\ne cv"stowter's booles: and it ends with the

)rst ewd, rcTeat.,qu ofth"irrstructions wili be followed for each customer. However, six

:nstructions will also be foilowed for each book the Customer wants signed.

Ben is sitting at a table in a bookstore, attending his book signing. He needs to sign zero or more

.rpi.. .i'r-,ii neitsettine ffk ; ,uoii.r for eich cusiomer standing in line'

: *,,.--.*,*,.--.'- i I follow these instructions i 1--: for each customer i

'*eai whLle ikere are ulslDweYs Lw LLvve: ' ,r4roa{or each of lne cuslow'er's boafes:

accqt ihe book' f row" l)'ae cLtstovwer'*-'i

TLace lhe boor< ow Lhe labLe

follow these instructionsDrpA.(he 1 t/t ca.(, o{ tre ao're followthese instructions sigw tnou.r vvrwLe Dwihe flrsLparle i i"1"9f3.j.P_9on"-.---*.-- .l close lhe bookz retu,rr" Llae bocla- to the ca"sLovwer

* -- -) uud YEeaL Lh o. wQ. th e c u-sta wer' " -'*-- *''

zwd rqeal

Fig*re ns-tr Problem specification and solution for signing zero or more books for each customer

E Cengage Learning 2013

What Time ls lt?

I clock uses nested loops to keep track of the time' For simplicity' consider a clock's minute and

second hands only. The second hand on a clock moves one position, clockwise, for every

second that has "lupr.J.

After the second hand moves 60 positions' the minute hand moves

one position, ,lro .lo.k*ise. The second hand then begins its journev around the clock again'

Figure 15-3 shows th; i";;,;;J uy u .to.t , *i,,.'te aid second hands As the figure indicates'

an outer ioop control, ili" -irr,rt"'hand, while the inner (nested) loop controls the second

hand. Notice that the entire nested loop is contained within the outer loop; this must be true

;br the loop to b. ,r"rt"d utd for it to *ort .o.,ectly' The next.iteration (repetition) of the

outer loop (which controls the minute hut'd) ottt-t's only after the nested loop (which controls

rhe second hand) has finished processing'

I'm on the lnside; You're on the Outside (Nested Loops)

i ,epert for vwLwt'tLes fron'. o tkrovt2)rt sJ: YePeai f or secowds frow" o tlnrowqk sJ:

nested loop I . T ove secowd' )nawd t posLtLow' cLcctsvtLse : ewd rePeat i wto,Je rwLwttte hawd t pcsLtLow, cLocQwLse

iF o'" A ,.aronl1,,.,..,_,*"'_, ,..,...-,.", - --* fi6irr* i*-* Logic used by a clock's minute and second hands O Cengage Learning 2013

Eigure 15-4 shows the interface for the Clock application. (The ciock image was downloaded

frJm the Open Clip Art Library at http://openclipart.org,) ln the Start button's Click event procedure, you *ili have an outer loop display the number of minutes, and a nested loop dlsplar ihe numbe. of seconds. For simplicity in watching the minutes and seconds tick away, you wiLl

display minute values from 0 through 2, and display second values from 0 through 5'

.flockE_+

Pvlinutes:

B

Seronds: fl

Ev!tStart

Iig:;ri* li-4 Clock application's user interface 0penClipArt.orgliltre

.i ,t :{': ' +li .?{',i

'*., Start Visual Studio 2012. Open the Clock Solution (Clock Solution.sln) file containec in the ClearlyVB20l2\Chap15\Clock Solution folder. If necessary, open the designer window.

;-:. Open the Code Editor window and locate the btnStart-Click procedure. Enter the oute: and nested ioops shown in Figure 15-5. The Refresh and Sleep methods are required s,-

that you can view each of the minute and second values in the interface.

One Loop Within Another LooP

i

]:- iFrivate S*b btn$tart-CLick(sender &e Object, e &s ' displays minut*s {fraru * thr*ugh 2 on}y} ' and secon*s {frcrn * through 5 *nly}

F*r intMinutes As Integ€r = 0 T* 2"*" lbLivlinutes. Text = intlvlinutes Fnr intSeconds As Int*ger = 6 T* 5

lblSecands.Text = intSeconds f*1*. Ref,resh( ) System. ThreadinE. Th*qa* ' S}.eep ( 5BB)

ill*xt intSeconds intMinute s

,

it

*-.. J-.*-. I

I

I the outer looP , i controls the minutes

'

iF{ext Lnd buB

, the nested loop i controls the seconds

Sigune 3S-S Start button's Click event procedure

3. Save the soLution and then start the appiication. Click the Start button. The number 0 appears in the lblMinutes control, and the numbers 0 through 5 appear (one at a time) in

the lblseconds control. Notice that the number of minutes is increased by 1 when the

number of second.s changes from 5 to 0. When the procedure ends, the lblMinutes and

lblseconds controls contain the numbers 2and5, respectively. (If you want to end the

procedure before it has finished. processing, click DEBUG on the menu bar and then

click Stop Debugging.)

4" Click the Exit button. Close the Code Editor window and then close the solution.

Mirti-QmBu ES-3

:ee Appendix B for the answers'

1. lf the outer loop is a pretest loop, then the nested loop --'

a. can be either a pretest loop or a posttest loop b. must be a Pretest looP c. must be a Posttest looP

2. Figure 15-6 shows a problem specification that requires a nested loop. The figure also .o.rt"in, an illustration of the problem along with a partial solution. Complete the

solution using only the additional instructions shown in the figure.

o+ Loops section in the

Ch1 5WantMore.pdf file.

&* ch15- ffix Nested

Loops

For more

examples

of nested

loops, see

the Nested