How to Use a Switch Statement

The switch statement is used in programming languages such as C, C++, Javascript and Java. When you desire to code a logical string of checking variable conditions and performing different actions depending on the value of the variable, you have two choices: 1) Using a string of if-then-else statements; or 2) Using a switch statement to execute a "case" value that matches the variable's value. The type of variable that a switch statement can check depends on the programming language. All languages are able to use the integer primitive data types. Java, which is used in this example, is able to use a switch statement for data types that include byte, short, char and int, as well as the object types character, byte, short and integer.

Things You'll Need

  • Java Development Environment
Show More

Instructions

    • 1

      Download and install the latest Java Standard Developer's Kit if not already installed.

    • 2

      Open a text editor and enter the following text to instantiate the demo code:
      Public class mySwitchDemo
      {
      Public static void

    • 3

      This example has nine integer values possible to switch on the assigned variable. Assign a value to the integer variable inning of four, followed by starting the switch statement to use that variable for the condition check.
      Int inning = 4;
      Switch (inning)
      {

    • 4

      The case statements compose the "switch" block of the switch statement. Each case must finish with the "break" statement, or the programming flow will automatically execute each successive case until it encounters a break or the final case is executed. In this example, the word "Fourth" will print to the command console when executed. The default case is executed if none of the previous case statements are used.
      Case 1: System.out.println("First"); break;
      Case 2: System.out.println("Second"); break;
      Case 3: System.out.println("Third"); break;
      Case 4: System.out.println("Fourth"); break;
      Case 5: System.out.println("Fifth"); break;
      Default: System.out.println("Extra Innings"); break;
      }// end Switch
      }//end Main
      }//end Class

Related Searches:

Resources

Comments

You May Also Like

  • How to Use a Switch Statement in C

    The switch statement is a powerful programming structure that exists in most programming languages. It will allow you to write a section...

  • How to Create a Switch Statement in C

    Use the Switch statement in C to evaluate a large number of values for a single variable or expression. Switch can be...

  • How to Use a Heapsort in Java

    The heapsort algorithm is one of the fastest sorting algorithms available. Programmers use heapsort in Java because it's a good choice for...

  • How to Use a Switch Case

    The switch-case statement in an important tool for cleanly controlling the flow of a program. It functions identically to a long list...

  • How to Use Switch Case in C#

    Using "If...else" throughout your C# application can be hard to read and may also lead to you making programming mistakes. Instead use...

  • How to Install an Ignition Switch in a 1983 Chevy C-10

    Replacing the ignition switch in your 1983 Chevy C-10 pickup presents a few challenges. Mounted on the steering column, the ignition switch...

  • How to Create a Switch Statement in JavaScript

    JavaScript's switch statement allows you to specify different actions to be taken based on different values of a variable or expression. Whereas...

  • How to Use the D-Link Switch

    Switches are networking devices that allow you to connect multiple computers. They offer the same peer-to-peer network connectivity function as routers and...

  • How to Use an If Then Statement in Excel

    The IF function, or statement, says to a computer program "if this happens, then do this." Many programming languages use IF statements...

  • How to Use a Switch in a Network

    A network switch is used to connect computers and other devices to a wired local network. Ethernet cables are usually used with...

Related Ads

Featured