Prev | Current Page 197 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

The cases can be ordered in any way that makes sense, even nonsequentially. The switch statement
executes by taking the value of the integral expression inside the parentheses (assume that dayOfChristmas
is an int). The switch statement then compares the value of the integral expression to each case value.
When it finds a match, the code for that case begins to execute.
In this example, the switch statement helps us write the lyrics for the song The Twelve Days of Christmas.
For instance, if the dayOfChristmas equals 3, execution will begin at case 3 and continue until it encounters
the break at the end of case 1. The one break statement is necessary to avoid executing the default
error message. The result will be the lyrics for the third day of Christmas:
Three French hens
Two turtle doves
And a partridge in a pear tree
CHAP. 5] PROGRAMMING IN JAVA 75
Here is the syntax for the switch statement:
switch( ) {
case value_one:
case value_two:
case value_three:
. . .
case value_n:
default:
}
The integral expression must evaluate to an integer value. This usually means that the integral expression
is an int or a char value.


Pages:
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
dieta light mieszkania życzenia pozycjonowanie wierszyki