For Loops

Note:  Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy

Part  1: The for loop

            Download the file, and change all of the counting while loops to be for loops, instead.  Make sure that the program still compiles, and continues to do the same thing it did before your modifications.

            Here is a quick picture to remind you how the two loops (while loops and for loops) are equivalent):

int i = 0;
while(i < 4)

     i++; // same as
          //  i = i + 1;

for(int i = 0; i < 4 ; i++)

     // no i++ here!




Part  2: The for loop & robots

            Locate a Java file that contains a robot within it.  This can be something you download from the website right now, or any file that you’ve previously downloaded.


            Use a for loop to have the robot move forwards 6 spaces.


            Once you’ve gotten that to compile and run, compare and contrast the while loop with the for loop.  In particular, be prepared to explain what sort of situation(s) a for loop is really good for, and what sort of situation(s) a while loop is really good for.

            Hint: When someone (usually a teacher J  ) tells you to ‘compare and contrast’, you should basically end up with two lists: how the two things are similar (this is the comparison), and how the two things are different (this is the contrast).


Part 3: For Loops: Practice Without Robots


            Goal: Be able to write a nested for loop from scratch, that doesn't use Robots


            Download the file  You'll notice that it's pretty much a bare-bones .Java file.  Your job is to create code in main that will print out a row of asterisks, like so:








            You can pick how long you want that line to be.  The important part is that I want you to be able to modify only one single line of code, and by modifying that single line, have the code print out as many asterisks as you want.

            Remember that if you want to print some text, without moving to the next line, you use

System.out.print("*"); // note that it's print, not printLN