Iteration Loops Loops repeat a set of instructions

  • Slides: 16
Download presentation
Iteration (Loops) • Loops repeat a set of instructions • Two types of loops:

Iteration (Loops) • Loops repeat a set of instructions • Two types of loops: – Definite loops ( for ) perform instructions explicit number of times – Indefinite loops ( while ) perform instructions an indefinite number of times (until a certain test fails) Ch. 6 1

Questions for You • a new class you’re writing has a method with the

Questions for You • a new class you’re writing has a method with the following signature: move (int num. Moves) – should it be implemented with a definite loop or an indefinite loop? justify your answer – could it be implemented with the other type of loop? if so, should we/could we/good idea/bad idea? ? ? Ch. 6 2

Questions for You • take 5 minutes and draw a situation on paper that

Questions for You • take 5 minutes and draw a situation on paper that would necessitate that you use a while loop to solve the problem Ch. 6 3

for Loops • General form: for ( <initialize variable>; <test>; <increment> ) { <instruction

for Loops • General form: for ( <initialize variable>; <test>; <increment> ) { <instruction set> } Ch. 6 4

for Loops (cont. ) • <initialize variable> sets a variable/identifier to a certain value

for Loops (cont. ) • <initialize variable> sets a variable/identifier to a certain value (variable is usually count, i, j) • <test> is a test that is evaluated each time the body is about to be executed (when false, loop is exited) • <increment> changes the loop control variable (usually x++ or x--) Ch. 6 5

for Loops (cont. ) • Example: for (int x=1; x<=5; x++) { karel. move();

for Loops (cont. ) • Example: for (int x=1; x<=5; x++) { karel. move(); } • This causes karel to move 5 times Ch. 6 6

for Loops (cont. ) Flow of for loops: 1. Initialization statement executes 2. If

for Loops (cont. ) Flow of for loops: 1. Initialization statement executes 2. If test is true, proceed to step 3; if test is false, go to step 6 3. Instructions in body of loop are executed 4. Increment statement executes 5. Return to step 2 6. Program proceeds to statements after loop Ch. 6 7

while Loops • General form: while ( <test> ) { <instruction list> } •

while Loops • General form: while ( <test> ) { <instruction list> } • Loop continues until test is false Ch. 6 8

Questions for You • back on slide 3 you drew up a situation that

Questions for You • back on slide 3 you drew up a situation that would best be solved using a while loop now write the code to solve your problem Ch. 6 9

while Loops (cont. ) • Example: while (karel. front. Is. Clear ()) { karel.

while Loops (cont. ) • Example: while (karel. front. Is. Clear ()) { karel. move (); } • Causes karel to move continuously until there is a wall in front of it Ch. 6 10

while Loops (cont. ) Flow of while loops 1. If test is true, proceed

while Loops (cont. ) Flow of while loops 1. If test is true, proceed to step 2; if test is false, go to step 4 2. Instructions in body of loop are executed 3. Go to step 1 4. Statements after loop are executed Ch. 6 11

Building a While Loop Formalizing (making easier) the ad-hoc way you’ve been writing while

Building a While Loop Formalizing (making easier) the ad-hoc way you’ve been writing while loops to this point. This will be extremely helpful to you at various times in the course and on the AP exam. • Step 1 – Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing) • Step 2 – Use the opposite form of step 1’s result as the <test> for the loop. You just need to negate the entire thing. (note: Now and again you might find that “De. Morgan-izing” your result is helpful when answering AP-like questions. ) Ch. 6 12

Building a While Loop (cont’d) • Step 3 – make progress toward the goal

Building a While Loop (cont’d) • Step 3 – make progress toward the goal (completion of the loop) within the loop • Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem • Example: Pick all beepers from a corner without knowing how many there are. Ch. 6 13

Let’s build a while loop Move a robot until it is not on a

Let’s build a while loop Move a robot until it is not on a corner with any other robots and there is a wall on the left, then pick up the beeper that will be waiting there (the “hard” part is getting the condition correct without using a trial-and-error method – you don’t get to run any programs during our tests nor on the AP exam – also, you’ll code your labs faster if you have a way to ensure correct conditions EVERY time – without the crutch of running your code) You may use && and || for this exercise. Use the 4 step process now… while (? ? ? ) { ? ? ? } Ch. 6 14

Your try the while condition You may use && and || for this Write

Your try the while condition You may use && and || for this Write a loop that moves a robot forward until it is either next to a robot, next to a beeper, or there is no wall on its right while ( ? ? ? ) { ? ? ? } Ch. 6 15

Infinite Loops • In a for or while loop, it is possible for the

Infinite Loops • In a for or while loop, it is possible for the test to never be false • When this happens, the loop continues infinitely • Depending on the compiler, application, and other circumstances, an error may occur or the app may crash Ch. 6 16