Control Statements

Control statements control the flow of the program.  So far we have written programs where we simply list all of the command and they are executed in order. If this is all we can do then we will be quite limited. For example, suppose we wanted to write a program which prints out the numbers from one to five. We might solve this as follows:

	public static void main(String[] args) 
	{
		System.out.println(1);
		System.out.println(2);
		System.out.println(3);
		System.out.println(4);
		System.out.println(5);
	}

Now while this does get the job done, it does require a bit of typing, and is not very flexible.  Suppose, for example, we needed to change this so that it printed out the number from one to a thousand instead.  Clearly we are no going to want to create a program with a thousand lines of code.

What we need is some way to cause one or more lines of our code to be executed repeatedly.  There are several ways this can be accomplished, but the one we are going to use here is a while loop.

The basic form of a while loop looks like:

while (condition)
{
   ...
}

For this control statement, all of the lines of code that are enclosed within the curly brackets will be repeatedly executed as long as condition is true.

Let’s rewrite the previous program using a while loop.

	public static void main(String[] args) 
	{
		int	count = 1;
		
		while (count <= 5)
		{
			System.out.println(count);
			
			count = count + 1;
		}
	}

Since we will need to count how many times we have written a line, we declare an int variable count to keep track, and we initialize it to the value 1.  Next we use the statement while(…) to start our loop.  This statement takes one argument which must resolve to a boolean type, and it will cause the block of code that follows (all of the code that is enclosed in curly brackets) to be executed as long as the boolean value is true.

So in this case, we want to execute the block as long as the count is less than or equal to 5.  The operator ‘<=’ is a comparison operator which will return true if, and only if, the number on the left is less than or equal to the number on the right.

Finally we define what code we want to be executed in the loop.  The first thing we need to do is print out the current value of count.  In addition we need to increment the value of count each time through the loop so that the loop will be executed only 5 times.  To do this we use the line:

			count = count + 1;

Now this line might look a little odd because your algebra will tell you that count can never equal count plus one.  The problem is that the equal sign, unlike in algebra, does not represent a comparison, but rather an assignment. What this line says is to take the current value of count, add 1 to it, and store that result back into the variable count.

Now if we run this program, we will get the following output:

1
2
3
4
5

As a side note, when we need to increment the count variable, we actually have a couple of other options:

			count++;
			count += 1;

Both of these lines will increment the variable count by one. Note that for the second line (as well as our original line) we can replace the one with any other number and increment count by any arbitrary value.

In this example we use the ‘<=’ operator, but there are a number of other comparison operators available to us:

  • a < b Will be true if a is less than b.
  • a <= b Will be true if a is less than or equal to b.
  • a > b Will be true if a is greater than b.
  • a >= b Will be true if a is greater than or equal to b.
  • a == b Will be true if a is equal to b.
  • a != b Will be true if a is not equal to b.

Now the control structure we are using here is quite common.  We initialize a variable, then check a condition and finally increment the variable at the end of the loop. It is so common in fact that there is a specific control statement that does all of these three thing for us.  We can change the above program to look like:

	public static void main(String[] args) 
	{
		int	count;
		
		for (count = 1 ; count <= 5 ; count++)
		{
			System.out.println(count);
		}
	}

Here we have replaced the while loop with a for loop.  This particular form of the for loop has 3 expressions separated by colons.  The first experssion is the initialization (in this case ‘count = 1’), the second expression is the condition (‘count <= 5’), the loop will continue as long as this condition is true. The final expression is the increment (‘count++’). If you now run this new program you will see that it produces the same output as the previous one. Note that there are other forms of the for loop that are useful, but we won’t be discussing those right now.

Next: if / else