Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language


Control Flow Statements

class Count {
    public static void main(String args[])
        throws java.io.IOException
    {
        int count = 0;

        while (System.in.read() != -1)
            count++;
        System.out.println("Input has " + count + " chars.");
    }
}    
The character-counting program uses a while statement to loop over all the characters of the input source and count them. Generally speaking, a while statement performs some action while a certain condition remains true. The general syntax of the while statement is:
while (expression)
    statement
That is, while expression is true, do statement. In the character-counting application, while the read() method returns a character that is not -1, the program increments count. More information about reading from the standard input stream is in the next page of this lesson.

Other Control Flow Statements

Statements such as the while statement are control flow statements. They determine the order in which other statements are executed. The Java language supports several other control flow statements:
   Statement             Keyword
decision making    if-else, switch
loops              for, while, do-while
exceptions         try-catch-throw
miscellaneous      break, continue, label:, return
Although goto is a reserved word, the Java language does not support the goto statement.


Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language