
The while loop
So far, we have been introducing programs that take as much input as specified. However, real life programs keep on asking for input and do much more repetitive tasks. Therefore, we will be introducing the concept of looping and iteration in this and the next tutorial. We will start now by introducing the while statement.
The while Statement
The while statement is somehow similar to the if statement. It has a condition that is checked and if the condition is true, the code in the while block will be executed. However, it differs from the if statement in that it keeps on executing the code inside it as long as the condition is true. Let's explain with an abstract example.
while(condition)
do something
Now, let's look at a real coding example.The following program will sum the numbers from one to ten.
public class While
{
public static void main(String args[])
{
int counter = 1;
int sum = 0;
while(counter <= 10)
{
sum += counter;
counter++;
}
}
}
In this program, we declared two integers. The first one was counter which is used for adding and for the condition in the while loop. The other integer was sum which is used to store the sum of the integers from 1 to 10. Now, the while loop checks if the counter is less than 10. If so, it will execute the code in its block. Each iteration we add counter to sum and increment counter by one. When counter reaches 11, sum will contain the numbers summed from 1 to 10 and the while loop will terminate.
Sentinel Controled Loop
So far, we have seen how to do a while loop with a counter. Now, we will learn how to do a while loop that is controled by an action from the user and not a certain counter. For example, let's return to summing up numbers. We want to keep reading from the user numbers and sum them up until a negative number is entered. The number will be called a sentinel because it is responsible for terminating the loop. Let's look at the program below.
import java.util.Scanner;
public class SentinelWhile
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int num = 0;
int sum = 0;
while(num >= 0)
{
sum += num;
num = input.nextInt();
}
}
}
In the previous program, we defined two integers. num will have the value entered by the user and sum will contain the value of the sum of positive numbers entered by the user. The while loop checks if num is not negative and keeps on adding num to sum and reading num from the user untill the user enters a negative number.
Congratulations
You now know how to iterate using a while loop. Try different stuff with the while loop like multiplying numbers entered by the user or terminating a while loop if a user enters an even number. These would be some fun exercises. You can move on to the next tutorial but first check the following important notes.
Important Notes
Be Careful When Looping
When dealing with loops, a programmer may fall into an infinite loop. This means that the program will keep on executing and can't be stopped which may cause severe errors to the output and the system. Therefore, always be careful when coding a while loop by checking if it will eventually terminate.
Reading Input in a Loop
When reading input from the user in a loop, always read it at the end of the loop so that if that input is a sentinel, it will not affect the output or computations in the loop.