Chapter 3.6 – The Switch Statement | Introduction to Programming Using Java

Chapter 3.6 – The Switch Statement | Introduction to Programming Using Java

 

3.6 The Switch Statement

 

The second branching statement in Java is the switch statement, which is introduced in this section. The switch statement is used far less often than the if statement, but it is sometimes useful for expressing a certain type of multi-way branch.

 

Chapter 3.6 - The Switch Statement | Introduction to Programming Using Java

 

3.6.1 The Basic switch Statement

 

A switch statement allows you to test the value of an expression and, depending on that value, to jump directly to some location within the switch statement. Only expressions of certain types can be used. The value of the expression can be one of the primitive integer types int, short, or byte. It can be the primitive char type.

Or, as we will see later in this section, it can be an enumuerated type. In particular, the expression cannot be a String or a real number. The positions that you can jump to are marked with case labels that take the form: “case hconstanti:”. This marks the position the computer jumps to when the expression evaluates to the given hconstanti. As the final case in a switch statement you can, optionally, use the label “default:”, which provides a default jump point that is used when the value of the expression is not listed in any case label.

A switch statement, as it is most oftern used, has the form:

 

Chapter 3.6 - The Switch Statement | Introduction to Programming Using Java

 

The break statements are technically optional. The effect of a break is to make the computer jump to the end of the switch statement. If you leave out the break statement, the computer will just forge ahead after completing one case and will execute the statements associated with the next case label.

This is rarely what you want, but it is legal. (I will note here—although you won’t understand it until you get to the next chapter—that inside a subroutine, the break statement is sometimes replaced by a return statement.)

Note that you can leave out one of the groups of statements entirely (including the break). You then have two case labels in a row, containing two different constants. This just means that the computer will jump to the same place and perform the same action for each of the two constants.

Here is an example of a switch statement. This is not a useful example, but it should be easy for you to follow. Note, by the way, that the constants in the case labels don’t have to be in any particular order, as long as they are all different:

 

Chapter 3.6 - The Switch Statement | Introduction to Programming Using Java

 

The switch statement is pretty primitive as control structures go, and it’s easy to make mistakes when you use it. Java takes all its control structures directly from the older programming languages C and C++. The switch statement is certainly one place where the designers of Java should have introduced some improvements.

 

the switch statement

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

1 thought on “Chapter 3.6 – The Switch Statement | Introduction to Programming Using Java”

Leave a Comment