Java while Loop
By Stephen Bucaro
A loop is a block of statements that are repeated. A loop may be repeated a fixed number of
times, repeat until some condition is met, or can loop indefinitely while the program is running.
There are three main loop control structures available in Java: for, do and while.
The most basic loop structure is the while loop. Shown below is an example of a while loop.
while(iVal < 10)
{
iVal++;
}
The while loop starts out with a conditional test. In the example above the conditional test
checks if the value in the variable iVal is less than 10. While this condition is true,
the while loop continues. Inside the code block of the while loop, the value of iVal
is incremented (increased) by 1. The value of iVal will be incremented each time the while
condition is true. On the tenth iteration through the loop, the value of iVal will
not be less than 10, so the condition in the while text will turn false and the loop will exit.
Note one important thing about the while loop is that one or more variables are set up before
the while statement. The test on those variables is performed before the code in the code block
is executed. This is in contrast to the do while loop where the code in the code block is
executed before the while test is performed.
The table below shows the operation of the while loop in the above example.
iVal | result | code block |
0 | true | inc iVal to 1 |
1 | true | inc iVal to 2 |
2 | true | inc iVal to 3 |
3 | true | inc iVal to 4 |
4 | true | inc iVal to 5 |
5 | true | inc iVal to 6 |
6 | true | inc iVal to 7 |
7 | true | inc iVal to 8 |
8 | true | inc iVal to 9 |
9 | true | inc iVal to 10 |
10 | true | exit loop |
So, the resulting function of this while loop example is to generate the numbers
1 through 10. Shown below is the complete code for an application that displays the output
in a program window.
import javax.swing.*;
public class whileLoop
{
public static void main(String s[])
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setTitle("Java while Loop Test");
String outData = "";
int iVal = 0;
while(iVal < 10)
{
iVal++;
String strI = String.valueOf(iVal);
outData += strI + " ";
}
JTextArea myText = new JTextArea();
myText.setLineWrap(true);
myText.append(outData);
frame.add(myText);
frame.setVisible(true);
}
}
In the code above, the code in the while loop code block uses the valueOf method
to convert iVal to a string and then concatenates it onto the end of the string variable
outData. A JTextArea object named myTextis created, and the line
myText.append(outData) places the outData string in the JTextArea for
display in the program window.
Note: I generally hate it when people add a bunch of extra code to an example. It confuses things.
But in this case I've included the javax.swing library and added code to create a
JFrame and a JTextArea. If I didn't add this extra code, you would need to use
the System.out.print command to output text, and I don't think that's an interesting
or realistic way to learn Java programming.
If you're a beginner, for this example, I recommend downloading and installing the Java Development Kit from
Oracle's web site.
Type the code into a text file using a basic text editor like Window's Notepad (Do not use a word processor
because they insert formatting codes into the file), saving the file with the .java file extension. Then
use a batch file to compile the program. The batch file needs to contain the path to the compiler and the
path to the source file, an example of compile.bat is shown below.
c:\Program Files\java\jdk1.8.0_121\bin\javac c:\Users\Stephen\Desktop\program_name.java
pause
The pause command on the second line will cause the DOS shell to stay open until you press any key,
allowing you to read any errors. If successful, the compiler will create an executable file with the
.class file extension. Then use a batch file to run the program. The batch file needs to contain
the start command, the name of the java program launcher, and the name of the compiled java class
file (without the .class extension). An example of run.bat is shown below.
start javaw program_name
The javaw launcher displays the result of your program, or error information if it fails, in a window.
|