Sunteți pe pagina 1din 9

224 | Page

Chapter 8 Loops

Continue Statement
The continue keyword is similar to break except that instead of halting execution of the loop entirely, the loop starts over at the next iteration. /* This FOR loop says to do looping until the value of the index variable is greater than 1000. However, if index equals 400, the continue statement causes the loop to go back to the "for statement" instead of the System.out.println statement. (continues with next iteration of loop and skips the printing for index =400.) */ for (int index = 1, index <= 1000; index ++) { if (index = = 400) continue; System.out.println("The index is " + index); }

8-7

Hands-on Projects Alice Loop Program

Project 1:
1.

2.

Open Alice 3. Set the scene with a female adult (Person) to be called brianne on grass and a chicken (Generic Alice models - animal) to be called chicken. Place brianne on the left of screen. Place the chicken on the right of screen. Bring them forward in scene so you can see them easily. The story board is that every day brianne does the following exercises:
10 situps 5 legLifts 15 toe touches

3.

You could code 15 statements of situps but that would just be tedious. Instead, a loop will be better. Since you know exactly how many situps will be done, the correct Alice looping statement would be count with Other Integer of 10 as follows:

Daly & Wrigley

225 | Page

Chapter 8 Loops

4.

The control structure for the loop has been set up. Now, drag the doSitUp method into the count loop as follows:

5. 6.

7.

Now, try setting up the 5 leg lifts and 15 toe touches on your own. Each of these sets of exercises are done in separate loops because they are completely separate (not alternating). If you want to see brianne do a sit up then leg lift and then a toe touch, move those statements inside of the first loop and eliminate the other 2 loops. Try your own combinations. Now, add the statements that would make the chickens head peck up and down 3 times. If you cant see the drop down list of PARTS, widen that left window until you can see it clearly. Your code should look something like the following:

Daly & Wrigley

226 | Page
8.

Chapter 8 Loops

9.

Add a hole to your scene. This is done by selecting the Generic Alice models shapes and then choosing Circle class. Name the circle as hole. You could color this hole as black or leave it alone as white. If your hole looks a bit grainy, move the hole up because this means the hole is down below ground level. The earlier statements in this project were done an exact number of times so the count statement was used. To have the girl, brianne, move to the hole and fall in, we really dont know how far she needs to move. Thus, we will have her turn to face the hole and then set up a while true block as follows:

10.

We want the while loop to be executed as long as brianne is not at the whole. Click on the function list for brianne and you will see a function called isAtLeastThresholdAwayFrom and you need to drag that over to the true in the while statement . It will then ask you how far do you want to be away and that should be 1 (meter) and away from what should be the hole. Your while statement should now look as follows:

11.

Inside of the while loop, brianne should move forward 1 meter. The while statement will cause this statement to repeat until she is within 1 meter of the hole s center.

12. 13.

Run your code to see what happens. You may want to right click on your count loops and disable them so that you dont have to watch the situps and chicken pecking, etc. One last thought is that we want her to fall through the hole. To do this, add this statement after the entire while loop:

14.

Now, the ending code should look as follows:

Daly & Wrigley

227 | Page

Chapter 8 Loops

15. 16. 17.

She should walk gradually to the hole and then move down so that she appears to be falling through the hole. Run this program moving brianne to different locations and see if she always gets to the hole and falls in ---- no matter what the distance. To see how these Alice statements would look in Java, select the WINDOW menu, then choose PREFERENCES and then PROGRAMMING LANGUAGE. Select JAVA as the programming language. The statements should now change to Java code with for loops, curly braces, etc. It should look as follows:

Daly & Wrigley

228 | Page

Chapter 8 Loops

Daly & Wrigley

229 | Page

Chapter 8 Loops

Project 2: Java For Loop to Print Numbers


1. Open NetBeans and begin a new project called PracticeLoops. 2. This is a very simple program with just a main method with a for loop inside of the main method. Type the following Java program into NetBeans. When you get to typing the for statement, try dragging the Counted For Loop over into your statements from the palette on far right side. It will ask for the variable you want to use and you should use i.

// // // //

This program is practice with for loops written by Your Name written on Date written with JDK version --

public class PracticeLoops { public static void main (String args [ ] { for (int i = 1; i < 5; i++) {

System.out.println("The number is " + i ); } // ends for block }// ends main }// ends program
3. Compile this program. Look at the for loop and determine what you believe the program will do. How many lines will it print? What will be on each of these lines? Execute the program and see if you were right. Now, execute the program to see if you were right. 4. The printout should be:

The number is 1 The number is 2 The number is 3 The number is 4 5. The for loop currently begins i at 1, adds 1 to i each time through the loop and will go as long as i is less than 5 but not including 5. Thus, it will print for i being 1, 2, 3, and 4 but ends the loop as soon as i becomes 5 and does not include 5. 6. What would we adjust if you wanted the loop to include 5? Adjust the for statement to include 5 by typing the following line: for ( int i = 1; i <= 5; i++)
Daly & Wrigley

230 | Page

Chapter 8 Loops

Or you could type the following line and it would accomplish the same thing: for ( int i = 1; i <6; i++) 7. How could you adjust the for statement so that it would print the odd numbers from 1 to 10. You should try to type in the for statement that you believe will work. 8. How could you adjust the for statement so that it would print the even numbers from 2 to 20. You should try to type in the for statement that you believe will work. One possibility is: for (int i = 2; i <=20; i+=2)

This will begin i at 2, increment i by 2 each time through the loop and go to and including i being 20. There are other correct solutions.

Project 3: Java Loop to Print 99 Bottles of Soda Song


1. Open NetBeans and begin a new project called BottlesOfSoda. 2. This program is modeled after the 99 Bottles of Beer song. We are going to use soda instead of beer. Add your comments to the top of the program.

3. This song repeats the following verse over and over again until you get to 0 bottles of soda. 99 bottles of soda on the wall, 99 of bottles of soda. Take one down and pass it around, 98 of bottles of soda on the wall. 4. Since this song repeats this verse over and over again, we can write the verse in a method so that it can be reused.

5. We need to call this printBottlesVerse from the main method. You need to pass a whole number into the printBottleVerse. Pass 99 for now as shown below:

Daly & Wrigley

231 | Page

Chapter 8 Loops

This will give you the following output:

6. We do not want to call this method with 98, 97, 96, 95, 94. This would be very inefficient. We could use a for loop to repeat this verse. We would start the for loop at 99 (int x=99) since that is what number the song starts with and we would subtract 1 from our number each time through the loop (x--). We would continue the loop (Boolean condition/test) until the number got to 0 (x > 0). Add the following code:

7. Run your program. It should print the song from 99 bottles to 0 bottles of soda. 8. The song should end with the following verse:

Add a println statement in the main method after the for loop. You dont want to put it in the for loop or else it will print this statement after each verse of your song.

9. Your finished program should look as follows:


Daly & Wrigley

232 | Page

Chapter 8 Loops

Daly & Wrigley

S-ar putea să vă placă și