System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə17/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   13   14   15   16   17   18   19   20   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence 
}
For versions of Java prior to JDK 7, the expression controlling the switch must resolve
to type byte, short, int, char, or an enumeration. (Enumerations are described in
Chapter 12
.) However, beginning with JDK 7, expression can also be of type String.
This means that modern versions of Java can use a string to control a switch. (This
technique is demonstrated in 
Chapter 5
, when String is described.) Frequently, the
expression controlling a switch is simply a variable rather than a larger expression.
Each value specified in the case statements must be a unique constant expression
(such as a literal value). Duplicate case values are not allowed. The type of each value
must be compatible with the type of expression.
The default statement sequence is executed if no case constant matches the
expression. The default is optional; if it is not present, no action takes place if all
matches fail. When a match is found, the statements associated with that case are
executed until the break is encountered or, in the case of default or the last case,
until the end of the switch is reached.
The following program demonstrates the switch:


The output produced by this program is shown here:
As you can see, each time through the loop, the statements associated with the case
constant that matches i are executed. All others are bypassed. When i is five or greater,
no case statements match, so the default statement is executed.


Technically, the break statement is optional, although most applications of the switch
will use it. When encountered within the statement sequence of a case, the break
statement causes program flow to exit from the entire switch statement and resume at
the next statement outside the switch. However, if a break statement does not end the
statement sequence associated with a case, then all the statements at and following
the matching case will be executed until a break (or the end of the switch) is
encountered.
For example, study the following program carefully. Before looking at the output, can
you figure out what it will display on the screen?
This program displays the following output:


As this program illustrates, execution will continue into the next case if no break
statement is present.
You can have empty cases, as shown in this example:
In this fragment, if i has the value 1, 2, or 3, the first println( ) statement executes. If it
is 4, the second println( ) statement executes. The “stacking” of cases, as shown in
this example, is common when several cases share common code.
NESTED SWITCH STATEMENTS
It is possible to have a switch as part of the statement sequence of an outer switch.
This is called a nested switch. Even if the case constants of the inner and outer
switch contain common values, no conflicts will arise. For example, the following code


fragment is perfectly acceptable:
Try This 3­1
Start Building a Java Help System
This project builds a simple help system that displays the syntax for the Java control
statements. The program displays a menu containing the control statements and then
waits for you to choose one. After one is chosen, the syntax of the statement is
displayed. In this first version of the program, help is available for only the if and
switch statements. The other control statements are added in subsequent projects.
1. Create a file called Help.java.
2. The program begins by displaying the following menu:
To accomplish this, you will use the statement sequence shown here:
3. Next, the program obtains the user’s selection by calling System.in.read( ), as
shown here:
4. Once the selection has been obtained, the program uses the switch statement


shown here to display the syntax for the selected statement.
Notice how the default clause catches invalid choices. For example, if the user enters
3, no case constants will match, causing the default sequence to execute.
5. Here is the entire Help.java program listing:


6. Here is a sample run.


THE FOR LOOP
You have been using a simple form of the for loop since 
Chapter 1
. You might be
surprised at just how powerful and flexible the for loop is. Let’s begin by reviewing the
basics, starting with the most traditional forms of the for.
The general form of the for loop for repeating a single statement is
for(initialization; condition; iterationstatement;
For repeating a block, the general form is
for(initialization; condition; iteration)
{

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   13   14   15   16   17   18   19   20   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə