Java Reference
In-Depth Information
While loop
TheJavawhilelooprepeatsastatementorstatementblock“while”acer-
tainconditionevaluatestotrue.Liketheforloop,thewhilelooprequires
initialization,processing,andtestingsteps.Thedifferenceisthatinthefor
loop,theinitializationandtestingstepsarepartoftheloopitself,butinthe
whileloopthesestepsarelocatedoutsidetheloopbody.Thefollowingpro-
gramusesawhilelooptodisplaytheASCIIcharactersintherange0x10to
0x20.
OntheWeb
TheprogramAsciiCodes.javaisfoundintheChapter10folderat
www.crcpress.com .
// File name: AsciiCodes.java
// Reference: Chapter 10
//
// Java program to demonstrate looping
// Topics:
//
1. Using the while loop
//
// Requires:
//
1. Keyin class in the current directory
public class AsciiCodes
{
public static void main(String[] args)
{
char value = 0x10;
while(value < 0x20)
{
System.out.println(value);
value++;
}
}
}
IntheprogramAsciiCodes.java,theinitializationoftheloopvariableis
performedoutsidethewhileconstruct.Theloopvariableisupdatedin-
sidetheloop.Theonlyloopelementcontainedintheloopexpressionis
thetestelement. Figure10-3 (onthenextpage)showstheelementsofthe
while loop.
Search WWH ::




Custom Search