Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
switch Statements
The if and else statements are good for situations with only two possible conditions, but there are times when you have more than two options that need to be considered. With the preceding grade example, you saw that if and else statements can be chained to handle several different conditions.
Another way to do this is to use the switch statement. You can use it in a Java program to test for a variety of different conditions and respond accordingly. In the following example, the grade example has been rewritten with the switch statement to handle a complicated range of choices:
switch (grade) {
case 'A':
System.out.println("You got an A. Great job!");
break;
case 'B':
System.out.println("You got a B. Good work!");
break;
case 'C':
System.out.println("You got a C. You'll never get into a good "
+ "college!");
break;
default:
System.out.println("You got an F. You'll do well in Congress!");
}
The first line of the switch statement specifies the variable that will be tested—in this example, grade. Then the switch statement uses the { and } brackets to form a block statement.
Each of the case statements checks the test variable in the switch statement against a specific value. The value used in a case statement must be either a character or an integer. In this example, there are case statements for the characters 'A', 'B', and 'C'. Each of these has one or two statements that follow it. When one of these case statements matches the variable listed with switch, the computer handles the statements after the case statement until it encounters a break statement.
For example, if the grade variable has the value of B, the text You got a B. Good work! will be displayed. The next statement is break, so no other part of the switch statement will be considered. The break statement tells the computer to break out of the switch statement.
The default statement is used as a catch-all if none of the preceding case statements is true. In this example, it will occur if the grade variable does not equal 'A', 'B', or 'C'. You do not have to use a default statement with every switch block statement you use in your programs. If it is omitted, nothing will happen if none of the case statements has the correct value.
The Conditional Operator | Next Section

Account Sign In
View your cart