While Loops


Introduction.  Now we want to present a bit more about looping when not as much can be pre-determined about the stopping and starting values.  This will fit under the aegis of while loops.

Here the looping is based on more abstract criteria so that the looping continues while an expression evaluates to true.

The standard syntax for a for loop conforms to the following

while (expression)


while (expression )

program statements
that are executed while
expression evaluates to true;



The criteria within the parentheses can be quite different. 

If there is only a single code statement after the while (expression) there doesn't need to be any braces.

while (expression )

single program statement executes at each iteration;

Some Verbal Examples.  Assume you have just bought a car on a five year plan with monthly payments.  Maybe you make some early payments or some payments that are larger than required.  You might get the following oversimplified code segment.

while (loanBalance > 0)

make a car payment


Well, this can go on and on.  Obviously, some of these depend on having other sources for inputs.

A Simple Code Example.  We will again find the sum of some integers starting at one.  But now we will ask the user whether they want to continue after each iteration.  You should call the application SumOfNAsYouGo.java.


import javax.swing.JOptionPane;

public class SumOfNAsYouGo

public static void main( String args[] )

int totalSum = 0;
int i = 0;
String strContinue;
int intContinue = 1;

JOptionPane.showMessageDialog(null, "You are about to sum the first N integers\nAs long as you choose to continue.", "Background", JOptionPane.INFORMATION_MESSAGE);

//  while loop that continues ass long as the user wants
while (intContinue == 1)

totalSum = totalSum + i;

strContinue = JOptionPane.showInputDialog("So far you have summed\nthe first " + i + " integers,\ngiving a total of " + totalSum + "\n\nEnter 1 to continue\nEnter 2 to quit");

intContinue = Integer.parseInt(strContinue);


JOptionPane.showMessageDialog(null, "The sum of the first " + i + "\nintegers is " + totalSum, "Solution", JOptionPane.INFORMATION_MESSAGE);

} // end main( )

} // end SumOfNAsYouGo


We do our typical imports, declarations and initializations so I won't discuss these any more.  We don't do our typical error trapping on the inputs and for the parsing.  Of greater interest is the while loop.
  • the while loop
    • starts at one
    • ends when the user inputs a number different from 1
    • increments in ones using the i++
  • the totalSum term is incremented each time through the loop by the value of the counter in the for loop thus achieving our goal of
    totalSum = 1 + 2 + ... + userStops
    when the loop is completed

  • the showInputDialog( ) method is used from the JOptionPane class to

    • display the results while echoing the input

    • ask the user if they want to continue

  • when the user decides to quit, one last showMessageDialog is used to present the final results to the user

There showInputDialog( ) looks largely like the following at each step.