Java Reference
In-Depth Information
code. In this case you can use a while loop in which the processing con-
tinues until the special ending code is detected.
The do-while loop provides an alternative to the while loop in which
the test condition is evaluated after the processing statements execute.
This ensures that the do-while loop executes at least once.
Programs often contain several loop constructs. Often one loop is
nested within another one. The program, named AsciiTable.java, listed
below, displays the table of ASCII characters in the range 0x20 to 0x7F.
The program uses four loops. The first two loops display the column
heads for the table. The third loop is a while loop which takes care of the
table's row heads and leading spaces. The fourth loop, nested in the third
one, displays a row of 16 ASCII codes. The program's result is a labeled
table of the ASCII character codes.
On the Web
The program AsciiTable.java is found in the Chapter 11 folder at
www.crcpress.com .
//
File name: AsciiTable.java
//
Reference: Chapter 11
//
//
Java program to demonstrate looping
//
Topics:
//
1. Using several loop constructs simultaneously
//
2. Nested loops
//
public class AsciiTable
{
public static void main(String[] args)
{
// Local variables
char hexLetter; // For table header
char ascCode = 0x20; // First ASCII code
// Counters for rows and columns
int row = 2;
int column;
System.out.print("\n\n");
System.out.print("
");
System.out.println("ASCII CHARACTER TABLE");
System.out.print("
");
System.out.println("characters 0x20 to 0xff");
System.out.print("\n
");
// Loops 1 and 2
// Display column heads for numbers 0 to F hexadecimal
for(hexLetter = '0'; hexLetter <= '9'; hexLetter ++)
System.out.print("
" + hexLetter);
Search WWH ::




Custom Search