# FAQ CS1 Week 5

### Muddiest Points from Lecture Material

1. Could you possibly give examples of times where while or do/while loops are better than for loops?

I've created two examples code/WhileSwitch.java and code/DoWhileSwitch.java that you can download and play with. Both use user input to control how long the loop continues, i.e., we don't know how many times the loop will execute when we wrote the program, so it doesn't make any sense to use a for loop, at least at the level we're talking about so far. (I.e., the for loop CAN be used for things other than counting loops as you become a more sophisticated programmer.)

In choosing between a while and a do/while loop, the question that could be asked is: "Is there any time I might NOT want to execute the body of the loop?" If the answer is yes, the while loop is a better choice because it is a pre-test loop (i.e., the boolean expresion is looked at BEFORE we enter the loop to determine IF we will enter it!). If the answer is no, we could use either. With experience you will learn to choose the most appropriate loop to keep your code readable!

2. What makes a loop a pre-test loop or a post-test loop?

When you test whether to continue the loop! In the case of both the while and the for loops, the boolean expression is evaluated BEFORE even thinking about entering the loop. If the expression evaluates to true, we then enter the loop. If not, we don't. Therefore, it is a pre-test look. (pre, before, get it?) However, in the case of the do/while loop, we ALWAYS execute the body of the loop at least once before evaluating the expression, i.e., the test is done AFTER the body of the loop has been executed, to see if we are going to continue. Hence, it is a post-test loop. (post, after, get it?)

3. If the for loop starts
for (int j = 0; j < 5; j++) {<
//do something
}
why doesn't j get set back to 0 each time through the loop?

That's simply the SEMANTICS of the for loop in Java. The expression before the first ; only gets executed the FIRST TIME you enter the loop. Because it is a pre-test loop, the second expresion then gets evaluated and tested. The third expression is evaluated AT THE END OF THE LOOP.

4. What does the j++ mean in the for statement above?

This was covered in week 2. The following three java statements are equivalent

• j = j + 1;
• j++;
• j += 1;

In class I suggest you limit the use of the last two versions for the sake of readability. The for statement for the loop is one case where I think it reasonable to use a shortened version.

5. What's the difference between the following two loops
for (int j = 0; j < 5; j++) {<
//do something
}

for (int j = 0; j <= 5; j++) {<
//do something
}

In the first loop, the loop control variable, j, takes on the values 0, 1, 2, 3, 4, and 5. It executes the body of the loop when j is 0, 1, 2, 3 and 4, but the does not when j is 5 as the conditional j < 5 fails at that point.

In the second loop, the loop control variable, j, takes on the values 0, 1, 2, 3, 4, 5, and 6. It executes the body of the loop when j is 0, 1, 2, 3, 4, and 5, but the does not when j is 6 as the conditional j <= 5 fails at that point.

So, the second loop executes one more time than the first one.

6. I don't understand nested loops. Can you help me out?

You can think of a nested loop like a clock. If we had a triply nested loop, the outer loop is like the hour hand and moves the slowest, i.e., we run through all the seconds for each minute in an hour before the hour hand changes. In the outer loop we do everything (i.e., run through all the two inner loops) before changing the loop control variable.

The middle loop moves the next fastest, just like the minute hand. The minute hand won't changed until the second hand has made it's circuit.

And, finally, the innermost loop could be compared to the second hand; it moves the fastest.

I've written a little program code/Clock.java that simulates the three clock hands using a for loop. Download it, compile it, and run it. If it gets too boring, just change some of the constants at the top of the program.

Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
October 5, 2004
http://www.cs.rit.edu/~ncs/Courses/cs1/Muddy4.shtml