Download presentation
Presentation is loading. Please wait.
Published byBrittany Booth Modified over 9 years ago
1
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com
2
2 Control structures IterationConditional MethodsSequential Iteration Graphic Organizer Types of loops Java statement Types of control structures Syntax Examples:
3
3 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for loops ) perform instructions an explicit number of times –Indefinite loops ( while loops ) perform instructions an indefinite number of times (until a certain test fails)
4
4 Definite vs. Indefinite Loop? A new class you’re writing has a method with the following signature: public void move (int numMoves) –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? Is it a good idea or bad idea?
5
5 Control structures IterationConditional Definite Loops MethodsSequential Iteration Graphic Organizer Indefinite Loops whilefor Types of loops Java statement Types of control structures Syntax Examples:
6
6 for Loops General syntax: for ( ; ; ) { }
7
7 for Loops (cont.) sets a variable/identifier to a certain value (variable is usually count, i, j) is a test that is evaluated each time the body is about to be executed (when false, the loop is exited) changes the loop control variable (usually i++ or i--). The incrementing is done at the bottom of the loop. i++ means “add 1 to i”. i-- means “subtract 1 from i”.
8
8 for Loops (cont.) Example: for (int i=1; i<=3; i++) { move(); putBeeper(); } This causes the robot to move and place a beeper down on 3 different corners Note: Write this on the board so that we can refer to it on next slide
9
9 for Loops (cont.) Flow of for loops: 1.Initialization statement executes 2.If test is true, proceed to next step; 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
10
10 Control structures IterationConditional Definite Loops MethodsSequential Iteration Graphic Organizer Indefinite Loops initialize variabletestincrement Types of loops Types of control structures Syntax Examples: for ( int i=1; i<=3; i++ ) { move(); putBeeper(); } whilefor Java statement
11
11 Move a specified number of times Example: public void move (int numMoves) { for (int i=1; ___________; i++) { super.move(); } } This causes the robot to move a specified number of times for (int i=1; i<=numMoves; i++)
12
12 Try this yourself Write a for loop to pick up a beeper (if there is one) on the four street corners directly in front of the robot: for (int i=1; i<=4; i++) { move(); if ( nextToABeeper() ) pickBeeper(); }
13
13 BeeperSquare Exercise
14
14 BeeperSquare Exercise Create a BeeperSquare robot class that extends SmartBot or Robot (you may want to use some of its methods) Create a method named drawSquare() that uses at least one “for” loop (two “for” loops would be better) that will create a square with 6 beepers on each side. Use a single robot to do this. The end result should be a square containing 20 beepers (only one beeper on each street corner). Create a client program that will test this new class. Try constructing two robots and drawing 2 separate squares in the world (overlapping the two).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.