Introduction to Computer Science - Java

Repeating the same action more than once

        We have seen that to get a computer to do what we want we must tell it every step, down to the last detail.   This can be very tedious.   It is especially tedious when we want it to do the same action several times.   For example in the chapter on sequence we wrote a recipie for a computer to mix a cake.   To get the computer to stir the bowl five (5) times, we had to write "stir contents of bowl with mixing spoon" five (5) times.   Wouldn't it be nice if we could tell the computer "Do the following action five (5) times: stir contents of bowl with mixing spoon."   This would certainly save us some typing.   Especially if we had wanted the computer to stir the bowl a hundred (100) times!  

        So how do we tell the computer to repeat an action many times?   Why not write something like what we saw above?

"Do the following action five (5) times: stir contents of bowl with mixing spoon."

        This would work for our case, but sometimes the situation is more complicated.   Really, we want to tell the computer "stir contents of bowl with mixing spoon until mixture is smooth."   This may take five mixes, but it may take ten or twenty.   If we generalize our case, we could say "Do something until something else changes."   Now, as we saw in computer programs we store "things" in variables.     We can refine our instruction to something like "Do something until variable X changes."

        But what does "changes" mean?   Changes in what way?   Variables are just numbers.   How do numbers change?   They can either increase or decrease.   So we could have some variable increase each time we stir the bowl, and then when that variable is larger than, say, five (5), we could end our action.   This is exactly how programs really work. Let's see an example first in English and then in Java.

make a variable and call it "counter"
assign the value "1" to the "counter"
do the following as long as counter is less than "6"
add "1" to counter
write this message to the screen "Hi mom!"

        This "English" is intended to be similar to a programming language but it should be understandable.   (Note: I gave the name "counter" to the variable, but I could have given any other name just as well.   I could have called it "George", but I like to give meaningful names.   This this variable is supposed to count, I called it "counter.")   Try to understand the logic of the above instructions.   How many times will the message get printed?   Now let's see this is Java.   Try to match up each line of English above with the Java below.

int counter;
counter = 1;
while ( counter < 6)
counter = counter+1;
System.out.println("Hi mom!");

        Can you match up the two versions?   Try again!   One thing should confuse you. What does this line mean: "counter = counter+1;"?   How can a variable equal the same variable plus 1?     That breaks the rules of algebra!   If counter is 1, well, 1 does not equal 1 + 1.   What is our mistake?     Our mistake is that we think = means equals, remember it means assign the value on the right side to the left side.   In our case that translates to assign the value 1 + 1 to counter.   Counter had been 1, now it will be 2.   Another way of thinking about what this line means is to translate it as follows: "the new value of counter will now be what the old value of counter was, plus one."   Think about this for a moment.

        You should notice that the two lines inside the braces { } are repeated 5 times.   How does this happen?   We said programs are executed in sequence, how can the computer execute the same line more than once?   What the computer does when it sees the closing brace } is to return to the condition inside the "while" line.  ("while" is another keyword.)   If that condition is true then the computer goes back into the code inside the braces { }.   (Code inside braces is called a code block or a code section.)   If the condition is not true, then it skips over the braces, just like in an " if ".

        The difference from "if" is that here the computer jumps up to the top of the "while" when it gets to the end.   The computer then re-checks the condition of the "while" to see if it is still true before proceeding to do the code section again.   In an "if" once the code inside the braces is execute, the computer never comes back to it.   If we think of a "while" as a way to get the computer to go down and then jump up to the top again, we can visualize a kind of looping effect.   Programmers call this construction a "loop".

        Terms to remember: counter, while, code section, and loop.
© Nachum Danzig September 2003