Chapter 3.1.2 – The Basic While Loop | Introduction to Programming Using Java

Chapter 3.1.2 – The Basic While Loop | Introduction to Programming Using Java

 

3.1.2 The Basic While Loop

 

The block statement by itself really doesn’t affect the flow of control in a program. The five remaining control structures do. They can be divided into two classes: loop statements and branching statements.

You really just need one control structure from each category in order to have a completely general-purpose programming language. More than that is just convenience. In this section, I’ll introduce the while loop and the if statement. I’ll give the full details of these statements and of the other three control structures in later sections.

 

the basic while loop

 

A while loop is used to repeat a given statement over and over. Of course, it’s not likely that you would want to keep repeating it forever. That would be an infinite loop, which is

generally a bad thing. (There is an old story about computer pioneer Grace Murray Hopper, who read instructions on a bottle of shampoo telling her to “lather, rinse, repeat.” As the story goes, she claims that she tried to follow the directions, but she ran out of shampoo. (In case you don’t get it, this is a joke about the way that computers mindlessly follow instructions.))

To be more specific, a while loop will repeat a statement over and over, but only so long as a specified condition remains true. A while loop has the form:

 

Chapter 3.1.2 - The Basic While Loop | Introduction to Programming Using Java

 

The semantics of this statement go like this: When the computer comes to a while statement, it evaluates the hboolean-expressioni, which yields either true or false as the value. If the value is false, the computer skips over the rest of the while loop and proceeds to the next command in the program. If the value of the expression is true, the computer executes the hstatementi or block of hstatementsi inside the loop.

Then it returns to the beginning of the while loop and repeats the process. That is, it re-evaluates the hboolean-expressioni, ends the loop if the value is false, and continues it if the value is true. This will continue over and over until the value of the expression is false; if that never happens, then there will be an infinite loop.

Here is an example of a while loop that simply prints out the numbers 1, 2, 3, 4, 5:

 

Chapter 3.1.2 - The Basic While Loop | Introduction to Programming Using Java

 

The variable number is initialized with the value 1. So the first time through the while loop, when the computer evaluates the expression “number < 6”, it is asking whether 1 is less than 6, which is true. The computer therefor proceeds to execute the two statements inside the loop. The first statement prints out “1”. The second statement adds 1 to number and stores the result back into the variable number; the value of number has been changed to 2.

The computer has reached the end of the loop, so it returns to the beginning and asks again whether number is less than 6. Once again this is true, so the computer executes the loop again, this time printing out 2 as the value of number and then changing the value of number to 3. It continues in this way until eventually number becomes equal to 6.

At that point, the expression “number < 6” evaluates to false. So, the computer jumps past the end of the loop to the next statement and prints out the message “Done!”. Note that when the loop ends, the value of number is 6, but the last value that was printed was 5.

By the way, you should remember that you’ll never see a while loop standing by itself in a real program. It will always be inside a subroutine which is itself defined inside some class. As an example of a while loop used inside a complete program, here is a little program that computes the interest on an investment over several years. This is an improvement over examples from the previous chapter that just reported the results for one year:

 

Chapter 3.1.2 - The Basic While Loop | Introduction to Programming Using Java

 

You should study this program, and make sure that you understand what the computer does step-by-step as it executes the while loop.

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

1 thought on “Chapter 3.1.2 – The Basic While Loop | Introduction to Programming Using Java”

Leave a Comment