Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java switch case Control Statement

A computer program is a list of instructions for the computer to follow. Some instructions tell the computer to test a variable and to follow a group of instructions based on the result of that test. This type of instruction is called a conditional test.

The if else statement tells the computer to run one set of instructions based on one result of a test, and a different set of instructions based on a different result of the test. But a switch case statement can run a different set of instructions based on any value or expression provided to the switch command.

import javax.swing.*;

public class ifelseCont2 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Java ifelse Test");

      int height = 75;
      String tall = "";

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);

      if(height < 74)
      {
          tall = "Your height is too short.";        
      }
      else if(height > 75)
      {
          tall = "Your height is too tall.";        
      }
      else
      {
	  tall = "Your height is just right."; 
      }

      myText.append(tall);
      frame.add(myText);

      frame.setVisible(true);
   }
}

The if else statement shown above, if the integer variable height is set to anything below 74, displays the message "Your height is too short." If height is set to anything above 75, it displays the message "Your height is too tall." If height is set to 75, it displays the message "Your height is just right." You can do a lot with a stack of else if statements, but a cleaner way to perform the same operation is the switch case structure as shown below.

public class switchStruct 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Java ifelse Test");

      int height = 73;
      String tall = "";

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);

      switch(height)
      {
         case(72):
            tall = "Your height is too short.";
            break;
         case(73):
            tall = "Your height is too short.";
            break;
         case(75):
            tall = "Your height is too tall.";
            break;
         case(76):
            tall = "Your height is too tall.";
            break; 
         default:
            tall = "Your height is just right.";
      } 

      myText.append(tall);
      frame.add(myText);

      frame.setVisible(true);
   }
}

The first line of a switch case structure defines the name of the variable that the case statements will perform thier test against. In the code above, the instructions under the line case(72) and above the nearest break instruction, will be executed if the variable height is equal to 72.

You can use an expression (e.g. a+x) in the switch line of the switch case structure, but not a condition (e.g. if x > x). Each case in a switch case must be a value of the result in the switch line.

The advantage of the switch case structure over the else if structure is that you can have a case branch for each value, you don't have to use conditions like, for example if (height > 74 && height < 76), just use case(75).

Note: I generally hate it when people add a bunch of extra code to an example. It confuses things. But in this case I've included the javax.swing library and added code to create a JFrame and a JTextArea. If I didn't add this extra code, you would need to use the System.out.print command to output text, and I don't think that's an interesting or realistic way to learn Java programming.

In the code above, a JTextArea object named myTextis created, and the line myText.append(tall) places the tall string in the JTextArea for display in the program window.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268