While Statement Tutorial

The while statement is a looping statement that repeats a block of code as long as an expression evaluates to true. The while statement has a great deal of flexibility because you get to determine the conditions that stop the loop. Stopping the loop simply means that the expression portion will evaluate to false. The while loop is quite simple in its structure:
while (expression) {
    Statements are executed;
    Some logic to change the value of the expression;
    Some logic to call the break statement;
}




Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md WhileLoop
C:\Java>cd WhileLoop
C:\Java\WhileLoop>Notepad WhileLoop.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class WhileLoop {
    public static void main(String args[]) throws java.io.IOException {
        int x = 0;
        System.out.print("Use while like a for statement: ");
        while(x < 10) {
            System.out.print(x);
            x++;
        }
        System.out.println();

        x = 0;
        System.out.print("Use break to terminate a while loop: ");
        while(true) { // infinite loop
            if(x == 5) {
                break; // terminate the while loop
            }
            System.out.print(x);
            x++;
        }
        System.out.println("\n"); // two line feeds in one
	
        System.out.print("Would you like to terminate this program? (Y/N): ");
        while(true) { // infinite loop
            char inputChar = (char) System.in.read();
            if (inputChar == 'Y' || inputChar == 'y') {
                System.out.println("Whew... glad that\'s over with!");
                break; // terminate the while loop
            } else if (inputChar == 'N' || inputChar == 'n') {
                System.out.print("Really? Okay, how about now? (Y/N):");
            } else {
                System.out.print("What??? Would you like to terminate this program? (Y/N): ");
            }
		
            // when you type a letter into the console the read() method buffers all keystrokes
            // If you type in N then press Enter read() buffers 'N' and '\n' (newline escape sequence)
            // this nested loop will empty any remaining characters in the read() buffer
            while(true) {
                char ignore = (char) System.in.read();
                if (ignore == '\n') {
                    break;
                }
            }				
        }
    }
}

Now switch back to the command prompt (CMD) and type in javac WhileLoop.java and press Enter.
Now type in java WhileLoop and press Enter.


C:\Java\WhileLoop>javac WhileLoop.java
C:\Java\WhileLoop>java WhileLoop
Use while like a for statement: 0123456789
Use break to terminate a while loop: 01234

Would you like to terminate this program? (Y/N): N
Really? Okay, how about now? (Y/N): W
What??? Would you like to terminate this program? (Y/N): Y
Whew... glad that's over with!


Final thoughts

The versatility of the while loop statement is super cool. Because it is so simple, the sky is the limit on how you choose to utilize its looping feature.


Tutorials