The Nuts and Bolts of the Java Language |
The character-counting program uses aclass 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."); } }while
statement to loop over all the characters of the input source and count them. Generally speaking, awhile
statement performs some action while a certain condition remains true. The general syntax of thewhile
statement is:That is, while expression is true, do statement. In the character-counting application, while thewhile (expression) statementread()
method returns a character that is not -1, the program incrementscount
. More information about reading from the standard input stream is in the next page of this lesson.Other Control Flow Statements
Statements such as thewhile
statement are control flow statements. They determine the order in which other statements are executed. The Java language supports several other control flow statements:AlthoughStatement Keyword decision making if-else, switch loops for, while, do-while exceptions try-catch-throw miscellaneous break, continue, label:, returngoto
is a reserved word, the Java language does not support thegoto
statement.
The Nuts and Bolts of the Java Language |