{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part36

Intro to Java Web-Notes_Part36 - The Switch Statement With...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Switch Statement With the switch statement the flow of execution is based on the value of a discrete value, such as the int, byte, short or char built-in data types. The switch statement cannot branch its flow of execuation via a range of data values, nor can the decision be base on f loat or doubles value. The general structure of the switch statement is: switch (expression) { case label: case labelZ: case labelB: default: When the program's flow of execution reaches the switch statement the expression is evaluated. The value of the expression is compared to the labels, one-at—a—time, from top to bottom. The first label that matches the value is the point at which the flow of execution begins again -- the statement associated with the matching label, and all statements from there to the end of the switch statement are performed. If the value does not match any of the labels Home - About Us :a:emeq:1; :a:emeq:2; :a:emeq: ; :a:emeq:4; :a:eme1: ; :a:eme1: ; :a:emeq:7; :a:emeq: ; then the flow of execution picks up with the "default" statement(s). For example char ch : 'c'; switch (ch) { case 'a' case 'c': case 'd': case 'e' default: In the above example the variable ch resolves to the character 'c'. This matches the second label, so the statement associated with that label, System . out . println ( " cat " ) , is performed, as well as all of the statements from there to the end of the switch statement. Therefore, the output of the above code fragment would be: System. System. System. System. System. out out out out. out. .print: print: .print: .print: _n("aardvark"); ,n("cat"); _H("d0g"); _n("elephant"); print: ,n("none"); eecti cat dog elephant none Usually a programmer would like the program to only perform the statement, or series of statements, associated with the matching label. This can be accomplished with brea k statements. When a brea k statement is encountered then the flow of execution skips to the end of the switch statement. The preceding code has been modified to include some break statements: switch (eh) i case 'a': System.out.println("aardvark"); break; case '0': System.out.print;n("cat"); System.out.print;n(" — a great petl"); break; case 'd': System.out.println("dog"); break; case 'e': System.out.println("elephant"); break; default: System.out.println("none"); Now the output when ch is a ’c’ is: cat — a great pet! If ch is a 'd' then the output would be "dog". If ch is an 'e' the output would be "elephant". If ch is a value that does not match any of the labels, such as a 'g', then the statement associated with default is executed -- in this case the output would be "none". More than one label can be stacked up on a statement: switch (eh) i case 'a case 2: case 'v'. System.out.println("aardvark"); break; case '0': System.out.println("cat"); System.out.println(" — a great petl"); break; case 'd': System.out.println("dog"); break; case 'e" System.out.println("elephant"); break; ...
View Full Document

{[ snackBarMessage ]}