These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

JAVA01/Switch Statements

Classnotes | JAVA01 | RecentChanges | Preferences

The if and else statements are good for situations with only a small handful of possible conditions. However, there will be times when you will need something which can provide you more options, that is where the switch statement comes in.

The basic structure of a switch statement is to provide some variable to test, and give several possible answers with a block of code associated with each of them. Here's an example:

 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. Eh.... it's okay");
      break;
    case 'D' :
      System.out.println("You got a D. Woah, that's not good.");
      break;
    default :
      System.out.println("You got an F. Dude, go flip burgers!");
      break;


Classnotes | JAVA01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited May 29, 2003 1:03 am (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.