CSCI A201/A597

Lecture Notes 4

Spring 2000


The for loop. Exercises. Homework assignment 1. The element package.

We start with what we were not able to cover last time.

The while loop should be pretty clear now. It represents a way of repeating the execution of a block (group) of statements for as long as a certain condition is true.

When a while loop is encountered in a program (which, I remind you, is executed sequentially, instruction after instruction) the condition is evaluated first. If it's false the body of the loop doesn't get executed. Otherwise the body is executed for as long as the condition evaluates to true.

The body of the loop must, of necessity, have an influence over at least one of the variables in the condition, directly or indirectly, otherwise we have an infinte loop.

We also need to distinguish the following parts in a loop:

Let's take an example:
int i = 1;              // initialization step 
int count = 0;          // initialization step 
while (i < 1000) {      // i >= 1000 is the termination criterion 
  if (i % 3  == 0) {    // body of loop 
    count = count + 1;  // body of loop 
  }                     // body of loop 
  i = i + 1;            // re-initialization step 
} 
This snippet of code computes (in count) the number of positive integers between 1 and 999 that are divisible by 3. We can use a similar loop structure, the for statement, that appears in the lecture notes of last time, to express the same iterative process that we have presented above using while. It would look as follows:

int count = 0; // intermediate result 
for (int i = 1; i < 1000; i = i + 1) {
  if (i % 3 == 0) {
    count = count + 1; 
  } 
} 
This is only a matter of re-organization. The syntax is different, the meaning is the same. Here are a few programming idioms that you are likely to see (and use) in Java programs:

i = i + 1;
can be written as
i += 1;
or even
i++;
The same is true for - (subtraction) and if we use any other expression instead of the 1 used in our example. You can read about this in your book on p. 80-83 and on p. 17.

Now let's practice this a little bit.

First, let's write a program that produces 10 lines of 10 asterisks each. The program would behave something like this:

frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
frilled.cs.indiana.edu%
One option would be to have 10 statements that each prints a line of 10 asterisks. We, of course, should rule this solution out as not satisfactory (since it won't scale up).

Here's an implementation using for:

public class One {
    public static void main(String[] args) {
        int lines = 10; 
        for (int i = 0; i < lines; i++) {
	    System.out.println("**********"); 
	} 
    } 
}
This is already better.

What do we need to do if we want to write 20 lines instead of 10?

What if we want to write a variable number of asterisks per line?

For example how would you write Two that behaves like this:

frilled.cs.indiana.edu%javac Two.java
frilled.cs.indiana.edu%java Two

*
**
***
****
*****
******
*******
********
*********
frilled.cs.indiana.edu%
It turns out that if we number the lines starting from 0 then on each line we need to write a number of asterisks that is equal to the number that the line has.

So the first thing is to be able to print the asterisks one by one rather than all at once. Here's, then, One.java again, but in this "finer granularity" form.

public class One {
    public static void main(String[] args) {
        int lines = 10; 
        for (int i = 0; i < lines; i++) {
	    for (int j = 0; j < 10; j++) {
		System.out.print("*");
	    }
	    System.out.println(); 
	} 
    } 
}
Can you see how the program works?

What is the change that we need to make to obtain the triangle?

Well, instead of printing 10 stars on each line we need to print a number of stars (asterisks) equal to the number of the line that is currently being printed.

The variable i, that starts at 0, is counting the lines. So all we need to do is to replace 10 in the second for loop with i and that should do the trick.

Try it.

OK, now how do you write Three?

frilled.cs.indiana.edu%javac Three.java
frilled.cs.indiana.edu%java Three
**********
*********
********
*******
******
*****
****
***
**
*
frilled.cs.indiana.edu%
And how about this?
frilled.cs.indiana.edu%javac Four.java
frilled.cs.indiana.edu%java Four
          
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
frilled.cs.indiana.edu%
Or this:
frilled.cs.indiana.edu%javac Five.java
frilled.cs.indiana.edu%java Five
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
frilled.cs.indiana.edu%
The program below is one of either Three, Four, or Five.
public class Think {
    public static void main(String[] args) {
        int lines = 10; 
        for (int i = 0; i < lines; i++) {
	    for (int j = 0; j < (10 - i); j++) {
		System.out.print(" ");
	    }
	    for (int j = 0; j < i; j++) {
		System.out.print("*");
	    }
	    System.out.println(); 
	} 
    } 
}
Can you guess which one? Can you write the other two?

Now we want to show you how you could use command line arguments with your program and then we will talk about your homework assignment. We will discuss one example, called Sum.java. Here's how we want it to behave:

frilled.cs.indiana.edu%pwd
/nfs/whale/home/user1/dgerman/a201/0120
frilled.cs.indiana.edu%javac Sum.java
frilled.cs.indiana.edu%java Sum 1 2 3
Your numbers add to: 6
frilled.cs.indiana.edu%java Sum 1 -1 2 -2 
Your numbers add to: 0
frilled.cs.indiana.edu%java Sum 2
Your numbers add to: 2
frilled.cs.indiana.edu%java Sum 1 4 7 -10 
Your numbers add to: 2
frilled.cs.indiana.edu%java Sum 1 2 3 4 5 6 7 8 9 10 11 13
Your numbers add to: 79
frilled.cs.indiana.edu%java Sum
Your numbers add to: 0
frilled.cs.indiana.edu%
Here's the program:
public class Sum {
    public static void main(String[] args) {
        int sum = 0; 
	for (int i = 0; i < args.length; i++) {
	    sum += Integer.parseInt(args[i]); 
	} 
	System.out.println("Your numbers add to: " + sum); 
    } 
}
This program: We will discuss these in class.

We will also show a prototype of the homework in class.

You are to write the program Syracuse which behaves like this:

frilled.cs.indiana.edu%javac Syracuse.java
frilled.cs.indiana.edu%java Syracuse 20 28
20: 7 iterations.
21: 7 iterations.
22: 15 iterations.
23: 15 iterations.
24: 10 iterations.
25: 23 iterations.
26: 10 iterations.
27: 111 iterations.
28: 18 iterations.
Max: 111 iterations, for [27]
frilled.cs.indiana.edu%java Syracuse 3 9
3: 7 iterations.
4: 2 iterations.
5: 5 iterations.
6: 8 iterations.
7: 16 iterations.
8: 3 iterations.
9: 19 iterations.
Max: 19 iterations, for [9]
frilled.cs.indiana.edu%
My prototype has 25 lines.

I will show you what the element package is about, but we will postpone working with it until next time.


Last updated: January 20, 2000 by Adrian German