CSCI 201 Introduction to Algorithm Design home | homework index | labs index FALL 2006

# Iteration

[an error occurred while processing this directive]

Repetition is something at which computers excel. In this lab, you will learn how to use Java's repetition structure -- the loop

## Repetition in Java

What do you think of when you hear the word Repetition? Doing the same thing over and over? While that might be the literal definition, doing the exact same thing over and over isn't very useful:

```    1 + 1 = 2
1 + 1 = 2
1 + 1 = 2
1 + 1 = 2
```

Now, that wasn't useful. However, if you allow a little feedback, repetition does become quite useful. Rather then adding 1 to itself again and again, this time add the result of the last addition to itself.

```    1 + 1 = 2
2 + 2 = 4
4 + 4 = 8
8 + 8 = 16
16 + 16 = 32
```

Now we have some useful work being done.

### Iteration in Java

In Java we have several forms of iteration, or repetition. One is the `while` loop. The `while` loop is done over and over again, until a desired result it accomplished. For example, suppose we need to generate a paycheck for every employee and that employee records are contained in a file called payroll.dat. The `while` loop for this task would look something like the following pseudo-code.

```    while (MoreEmployees("payroll.dat")) {
GeneratePayCheck(ER) ;
}
```

### A boring example from elementary school

Let's look at an example. Perhaps some of you were given the task of writing the numbers from 1 to 1000 when you were a student at Penrose Elementary School. (Or maybe not...)

This task can easily be accomplished with a single loop in Java.

```   int i=1;
while (i<=1000) {
System.out.println(i) ;
i = i+1 ;
}
```

Suppose a few years later, you are asked to write all the squares up to 10000 -- 1, 4, 9, 16, ...., 9604, 9801, 10000. The good news is that there are only 100 of them. The bad news is that you have to multiply 100 different numbers. Fortunately, you know Java.

```   int i=1;
while (i*i<=10000) {
System.out.println(i*i) ;
i = i+1 ;
}
```

### Java for loop

Java also has another form of iteration called the `for` loop. The `for` is very good for writing repetitive mathematical computations. It puts the loop initialization (`i = 1`), test (`i<=1000`), and update (`i = i+1`) into a single statement, as shown in the following examples:

```   for (i=1; i<=1000; i=i+1) {
System.out.println(i) ;
}
```
```   for (i=1; i*i<=10000; i=i+1) {
System.out.println(i*i) ;
}
```

### Being even more terse

Experienced Java programmers might express this even more succinctly by using the `++` operator and by taking advantage of the fact that a `for` loop with a one-statement body doesn't need the curly braces.

```   for (i=1; i<=1000; ++i)
System.out.println(i) ;
```
```   for (i=1; i*i<=10000; ++i)
System.out.println(i*i) ;
```

### The null statement pitfall

Programmers become accustomed to adding semi-colons to the end of lines and may accidently produce the following:

```   for (i=1; i<=1000; ++i) ;
System.out.println(i) ;
```

This is legal Java. The semi-colon at the end of the `for` is actually a null statement. It means that nothing is done in the body of the `for`. It is just like writing the following piece of code which only outputs the number 1001. See if you can figure out why.

```   for (i=1; i<=1000; ++i) {
}
System.out.println(i) ;
```

## More on for

Loops are very simple, yet powerful. There are three steps required to set up a loop. First, you initialize the loop variable. Next, you provide the loop test. Finally, you put in the update statement. This whole sequence of events is illustrated by the following Stack N' Heap:

 Code int i = 1; while (i < 32) { i = i + i; } Stack Heap Temporary

## Checkpoint

Answer the questions below. You have to answer all questions correctly to see the rest of the lab.

How many * will be displayed?

```	for ( i = 3;  i <= 13;  i += 3 ) {
System.out.println( "*" );
} ```

3
4
5
0

What will be the value of x after execution of the `while` loop?

```	     int x;
x = 1;
while (x <= 32) {
x = 2 * x;
}
System.out.println(x); ```

64
32
1
128