Variables and Arithmetic Expressions

We’ll begin this section by finding a more elegant way to handle multiple command line arguments of an undetermined number. Toward this end we introduce the concept of a loop. A loop is a section of code that is executed repeatedly until a stopping condition is met. A typical loop may look like:

while there's more data {
  Read a Line of Data
  Do Something with the Data
}

This isn’t working code but it does give you an idea of a very typical loop. We have a test condition (Is there more data?) and something we want to do with if the condition is met. (Read a Line of Data and Do Something with the Data.)

There are many different kinds of loops in Java including while, for, and do while loops. They differ primarily in the stopping conditions used.

For loops typically iterate a fixed number of times and then exit. While loops iterate continuously until a particular condition is met. You usually do not know in advance how many times a while loop will loop.

In this case we want to write a loop that will print each of the command line arguments in succession, starting with the first one. We don’t know in advance how many arguments there will be, but we can easily find this out before the loop starts using the args.length. Therefore we will write this with a for loop. Here’s the code:

// This is the Hello program in Java
class Hello {

    public static void main (String args[]) {

      int i;

      /* Now let's say hello */
      System.out.print("Hello ");
      for (i=0; i < args.length; i = i+1) {
        System.out.print(args[i]);
        System.out.print(" ");
      }
      System.out.println();
  }

}

We begin the code by declaring our variables. In this case we have exactly one variable, the integer i.

Then we begin the program by saying “Hello” just like before.

Next comes the for loop. The loop begins by initializing the counter variable i to be zero. This happens exactly once at the beginning of the loop. Programming tradition that dates back to Fortran insists that loop indices be named i, j, k, l, m and n in that order. This is purely a convention and not a feature of the Java language. However anyone who reads your code will expect you to follow this convention. If you choose to violate the convention, try to give your loop variables mnemonic names like counter or loop_index.

Next is the test condition. In this case we test that i is less than the number of arguments. When i becomes equal to the number of arguments, (args.length) we exit the loop and go to the first statement after the loop’s closing brace. You might think that we should test for i being less than or equal to the number of arguments; but remember that we began counting at zero, not one.

Finally we have the increment step, i=i+1. This is executed at the end of each iteration of the loop. Without this we’d continue to loop forever since i would always be less than args.length. (unless, of course, args.length were less than or equal to zero. When would this happen?).

Sidebar: Why Algebra teachers hate Basic and aren’t that fond of C

The statement i=i+1 drives algebra teachers up the wall. It’s an invalid assertion. There isn’t a number in the world for which the statement i=i+1 is true. In fact if you subtract i from both sides of that equation you get the patently false statement that 0 = 1. The trick here is that the symbol = does not imply equality. That is reserved for the double equals sign, ==. In almost all programming languages including Java a single equals sign is the assignment operator.

The notable exceptions are Pascal (and the Pascal derivatives Modula-2, Modula-3 and Oberon), Ada, and Eiffel where = does in fact mean equality and where := is the assignment operator. Math teachers are very fond of their equal sign and don’t like to see it abused. This is one reason why Pascal is still the most popular language for teaching programming, especially in schools where the Computer Science department is composed mainly of math professors.

Needless to say math professors hate languages like Basic where, depending on context, = can mean either assignment or equality.

Exercises
  1. What happens now if we don’t give the Hello program any command line arguments? We aren’t testing the number of command line arguments anymore so why isn’t an ArrayIndexOutOfBoundsException thrown?
  2. For math whizzes only: I lied. In certain interpretations of certain number systems the statement i = i + 1 does have a valid solution for i. What is it?

Comments are closed.