PART I
C h a p t e r 5 :
P r o g r a m C o n t r o l S t a t e m e n t s
89
PART IPART I
switch(
expression
) {
case
constant1
:
statement sequence
break;
case
constant2
:
statement sequence
break;
case
constant3
:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}
The
switch
expression must be of an integer type, such as
char
,
byte
,
short
, or
int
, of
an enumeration type, or of type
string
. (Enumerations and the
string
type are described
later in this book.) Thus, floating-point expressions, for example, are not allowed.
Frequently, the expression controlling the
switch
is simply a variable. The
case
constants
must be of a type compatible with the expression. No two
case
constants in the same
switch
can have identical values.
The
default
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.
The following program demonstrates the
switch
:
// Demonstrate the switch.
using System;
class SwitchDemo {
static void Main() {
int i;
for(i=0; i<10; i++)
switch(i) {
case 0:
Console.WriteLine("i is zero");
break;
case 1:
Console.WriteLine("i is one");
break;
case 2:
Console.WriteLine("i is two");
break;
case 3:
Console.WriteLine("i is three");
break;
www.freepdf-books.com
90
P a r t I :
T h e C # L a n g u a g e
case 4:
Console.WriteLine("i is four");
break;
default:
Console.WriteLine("i is five or more");
break;
}
}
}
The output produced by this program is shown here:
i is zero
i is one
i is two
i is three
i is four
i is five or more
i is five or more
i is five or more
i is five or more
i is five or more
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
constants
match, so the
default
is executed.
In the preceding example, the
switch
was controlled by an
int
variable. As explained,
you can control a
switch
with any integer type, including
char
. Here is an example that uses
a
char
expression and
char case
constants:
// Use a char to control the switch.
using System;
class SwitchDemo2 {
static void Main() {
char ch;
for(ch='A'; ch<= 'E'; ch++)
switch(ch) {
case 'A':
Console.WriteLine("ch is A");
break;
case 'B':
Console.WriteLine("ch is B");
break;
case 'C':
Console.WriteLine("ch is C");
break;
case 'D':
Console.WriteLine("ch is D");
break;
case 'E':
Console.WriteLine("ch is E");
www.freepdf-books.com
PART I
C h a p t e r 5 :
P r o g r a m C o n t r o l S t a t e m e n t s
91
PART IPART I
break;
}
}
}
The output from this program is shown here:
ch is A
ch is B
ch is C
ch is D
ch is E
Notice that this example does not include a
default
case. Remember, the
default
is optional.
When not needed, it can be left out.
In C#, it is an error for the statement sequence associated with one
case
to continue on
into the next
case
. This is called the “no fall-through” rule
.
This is why
case
sequences end
with a
break
statement. (You can avoid fall-through in other ways, such as by using the
goto
discussed later in this chapter, but
break
is by far the most commonly used approach.)
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
. The
default
sequence also must not “fall through,” and it too usually
ends with
break
.
The no fall-through rule is one point on which C# differs from C, C++, and Java. In
those languages, one
case
may continue on (that is, fall through) into the next
case
. There
are two reasons that C# instituted the no fall-through rule for
case
s: First, it allows the
compiler to freely rearrange the order of the
case
sequences, perhaps for purposes of
optimization. Such a rearrangement would not be possible if one
case
could flow into the
next. Second, requiring each
case
to explicitly end prevents a programmer from accidentally
allowing one
case
to flow into the next.
Although you cannot allow one
case
sequence to fall through into another, you can have
two or more
case
labels refer to the same code sequence, as shown in this example:
// Empty cases can fall through.
using System;
class EmptyCasesCanFall {
static void Main() {
int i;
for(i=1; i < 5; i++)
switch(i) {
case 1:
case 2:
case 3: Console.WriteLine("i is 1, 2 or 3");
break;
case 4: Console.WriteLine("i is 4");
break;
}
}
}
www.freepdf-books.com
92
P a r t I :
T h e C # L a n g u a g e
The output is shown here:
i is 1, 2 or 3
i is 1, 2 or 3
i is 1, 2 or 3
i is 4
In this example, if
i
has the value 1, 2, or 3, then the first
WriteLine( )
statement executes. If
i
is 4, then the second
WriteLine( )
statement executes. The stacking of
case
s does not violate
the no fall-through rule, because the
case
statements all use the same statement sequence.
Stacking
case
labels is a commonly employed technique when several
case
s share
common code. This technique prevents the unnecessary duplication of code sequences.
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.
The
case
constants of the inner and outer
switch
can contain common
values and no conflicts will arise. For example, the following code fragment is perfectly
acceptable:
switch(ch1) {
case 'A': Console.WriteLine("This A is part of outer switch.");
switch(ch2) {
case 'A':
Console.WriteLine("This A is part of inner switch");
break;
case 'B': // ...
} // end of inner switch
break;
case 'B': // ...
The for Loop
The
for
loop was introduced in Chapter 2. Here, it is examined in detail. 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
;
iteration
)
statement
;
For repeating a block, the general form is
for(
initialization
;
condition
;
iteration
)
{
statement sequence
}
The
initialization
is usually an assignment statement that sets the initial value of the
loop
control variable,
which acts as the counter that controls the loop. The
condition
is a Boolean
expression that determines whether the loop will repeat. The
iteration
expression defines the
amount by which the loop control variable will change each time the loop is repeated. Notice
that these three major sections of the loop must be separated by semicolons. The
for
loop will
www.freepdf-books.com
Do'stlaringiz bilan baham: |