Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

# Outline of Class 11: Looping

## Miscellaneous

• No office hours this afternoon.
• Any suggestions on Glimmer?
• Any questions on assignment two?

## While Loops

• Often, you want to execute a series of statements as long as a condition holds.
• For this case, Java provides the `while` loop, which has the form
`while (test) statement`
• In executing this loop, Java evalutes the test. If the test fails, the loop terminates. Otherwise, Java executes the statement and then goes back and tries the test again.
• Note that the statement can be empty.
• At times, you will want to have a loop in which the body is executed once before the test. While it is possible to do this by repeating code, it is easeier to use a variant of the `while` loop called the `do` loop, and has the form
`do statement while (test)`.

## For Loops

• At times, you want to execute a statement (or block of statements) a fixed number of times.
• For example, to calculate the factorial of N, we might want to multiply 1 by the numbers from one to N.
• For cases like this, Java provides the `for` loop, which has the form
```for (initialization; test; increment)
statement;
```
• In effect, this is equivalent to
```initialization;
while (test) {
statement;
increment;
}
```
• Note that this is somewhat more general than the `for` loop of Pascal.
• Any of the four parts of a `for` loop can be ommitted (in which case it is ignored).
• We can then solve our initial factorial problem as follows
```int i;		// A counter variable
int factorial;  // The factorial of N
factorial = 1;
for (i = 1; i <= N; i = i+1) {
factorial = factorial * i;
}
```
• Most Java programmers increment their counter variables with the postincrement operator, `var++`.

### Pre- and Post-increment

• In many ways, Java reflects the heritage of C.
• One place that this is particularly apparant is in the preincrement and postincement (and pre- and post-decrement) operations.
• In C, `++` can be used before or after any variable (or reference to a memory location). The effect is to increment the variable by one (or an equivalent of one).
• Similarly, `--` can be used to decrement the variable by one.
• In Java, you can use `++` and `--` with integer variables (and their variants).
• If the `++` comes before the variable, the value of the expression is the new value of the variable.
• If the `++` comes after the variable, the value of the expression is the old value of the variable.

### Exercise

Write a program that builds a 10x10 multiplication table. If you have time, you may wish to extend it to read the width and height from the command line.

The core of your program will most likely look something like the following

```for(row = 1; row <= width; ++row) {
// You may want to fill in something here
for(column = 1; column <= width; column = column + 1) {
// Fill in something here
} // for each column
// You may want to fill in something here
} // for each row
```

## Extended Loop Control

• At times, you need to exit your loops (or one repetition of a loop) early.
• For example, you might have noted an abnormal condition, or have reached termination.
• While it is always possible to provide for such early exits by intelligent use of conditionals, such conditionals can inhibit the readability of your code.
• Hence, Java provides methods for exiting loops.
• `break` exits the current loop (or switch).
• `continue` exits the current repetition of the loop (going back to the test, and, possibly, to another repetition of the loop).
• Since we often nest loops, Java also provides a mechanism for exiting nested loops. You can label a any of the loops or switches with `label: statement` and then use `break label` to break out of that statement. For example
```outer:
for (i = 0; i < N; ++i) {
inner:
for (j= 0; j < M; ++j) {
if (meetsCondition(i,j)) {
System.out.println(
"Found what we were looking for at (" +
i + "," + j + ")"
);
break outer;
} // if
} // inner for loop
// Here's where you go if you break out of the inner loop
} // outer for loop
// Here's where you go if you break out of the outer loop
```
• Similarly, `continue` can take a label, and returns to the next iteration of the labeled loop.

Outlines: prev next

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.