<< Return to Labs Page

Lab 7 - Help With Loops

Getting Started

Understanding The while Loop

What the code above does is give us two simple examples of loops: the top one is a while loop while the bottom one is a for loop. Start your thinking with the while loop. Really, this works like an if statement that gets repeated several times. First we check the top line of the while loop. This means checking whatever condition is between the parantheses: ( and ). In this case, the condition is: count < end.

If the condition count < end evaluates to true then the computer runs whatever code is in the body of the while loop. This is simply the code that comes between the curly braces, { and } that immediately follow this top line of the while loop. In this case, the body of the while loop only has two lines that might be run:

         System.out.println("Counter equals:  "+count);
         count = count + 1;

So, the body of the while loop is executed if the condition between the parantheses evaluates to true. If that condition instead evaluates to false then the body of the while loop is not run. So far, this is exactly like how an if statement works. The only change is that if the body of the while loop is run, after it's finished running we will repeat this whole process. This means that we again evaluate the condition, here that means checking count < end and if the condition is again true then we again run the body of the while loop. This can continue as long as the condition keeps evaluating to true. It could go on forever! In this particular code example that won't be the case, though, as one of the lines in the body of the code keeps incrementing (increasing by 1) the value of the variable count. So, eventually we will have count < end evaluate to false, and the loop will end.

Understanding The for loop

The for loop works just as the while loop does, it just uses some different syntax. This makes it a little more confusing at first, but can make the code a little bit shorter. The part that we need to focus on is the top line of this loop, it's header. It's header is divided up into 3 parts: initialization, condition, and increment. Here's a look at those parts of the for loop's header:

The first part of that header, the initialization, occurs only once and occurs before anything else. That's the highlighted part below.

The condition portion of the header is executed once before every iteration of the loop. If the condition evaluates to true then the body of the loop is run one more time. If the condition evaluates to false, the for-loop is done and we move on to whatever code happens to follow it. The highlighted part below is the condition statement.

Finally, the update portion of the header occurs once after every iteration of the loop. Typically, this portion of the header is used to increment the variable created in the initialization portion (which is then checked each time in the condition portion). The increment portion of the header is highlighted below.

In this case we have a simple example with a counter variable, variable i, starting with value 0, being incremented each time through the loop, and continuing so long as the value in variable i is less than or equal to (<=) the value 20. Instead of a set number 20, we could have used any numeric variable (type int, double, etc.).

It is possible to make the for loop a little more complicated. Any of the boolean expressions that worked for the condition header in if and while statements will also work for the condition part of the for loop's header. Also, while the increment part can be a simple addition statement, it could also make use of the more extravagant assignment operators you've may have seen in class lectures. Finally, note that this is one type of for loop. In lecture, Prof. Forbes also described a different type of for loop that is a little more specialized, but which you might find shorter and simpler. Those lecture notes are linked in PDF form and in PowerPoint form.

Extra for Loop Stuff

This is some extra information in case you want to try more extravagant things with for loops in the future. It is also possible to modify the increment variable within the body of the for-loop. You must take extreme care when doing this, though, because you must always make sure that your loop will eventually terminate by causing the condition portion of the header to evaluate to false. The initialization variable also does not have to be declared inside the header of the for-loop. It can be declared in code preceding the for-loop.

This can be useful because variables declared in the header or body of the for statement are thrown away and lost when the statement finishes executing. To save the variable - and whatever value it might finish the loop holding - we'd need to declare it before the loop. This instance of a variable being thrown away is not unique. All variables only exist within a certain realm, typically within innermost curly braces that they were declared within. The exact details of may be covered in later lectures, or you can ask your TA or professor.