BLOCKS AND STATEMENTS
The
while
Statement
14.10
class Twomany {
static void howMany(int k) {
switch (k) {
case 1:
System.out.println("one");
break;
//
exit the switch
case 2:
System.out.println("two");
break;
//
exit the switch
case 3:
System.out.println("many");
break;
//
not needed, but good style
}
}
public static void main(String[] args) {
howMany(1);
howMany(2);
howMany(3);
}
}
This program prints:
one
two
many
14.10 The
while
Statement
The
while
statement executes an
Expression
and a
Statement
repeatedly until the
value of the
Expression
is
false
.
WhileStatement:
while (
Expression
)
Statement
WhileStatementNoShortIf:
while (
Expression
)
StatementNoShortIf
The
Expression
must have type
boolean
, or a compile time error occurs.
A
while
statement is executed by first evaluating the
Expression
. If evalua
tion of the
Expression
completes abruptly for some reason, the
while
statement
completes abruptly for the same reason. Otherwise, execution continues by mak
ing a choice based on the resulting value:
If the value is
true
, then the contained
Statement
is executed. Then there is a
choice:
N
If execution of the
Statement
completes normally, then the entire
while
statement is executed again, beginning by re evaluating the
Expression
.
277
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved