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 of \"if then else\" statements, however its more compact and readable structure makes it more suitable when a long list of tests must be performed on a single variable. The switch-case statement will be covered in three major languages: Java, C and Pascal.

Instructions

  1. Java

    • 1

      Create the switch statement and open the bracket. The variable being analyzed must be of the int primitive data type. Doubles, floats or other data types cannot be used in a Java switch statement.<br /><br /> switch(x) {

    • 2

      Create the conditions and add the break keyword.<br /><br /> case(1):<br /> System.out.print(\"1\")<br />GO<br /> break<br />GO<br /> case(2):<br /> System.out.print(\"2\")<br />GO<br /> break<br />GO<br /><br />Be careful here. Failure to use the break keyword will cause the code to run all the other case instructions until it hits a break. For example, if the break in case(1) is removed and x is equal to 1, then the output will be \"1 2\" rather than simply \"1.\"

    • 3

      Create a default condition and close the bracket.<br /><br /> default:<br /> System.out.println(\"Unknown.\")<br />GO<br /> break<br />GO<br /> }<br /><br />The default condition will be run if no other condition matches.

    C / C++

    • 4

      Create the switch-case statement. It is almost identical to how it appears in Java, except there are no parenthesis surrounding the values being tested for in the case statements.<br /><br />switch(x) {

    • 5

      Add the case statements.<br /><br />switch(x) {<br /> case 1:<br /> cout << \"1\"<br />GO<br /> break<br />GO<br /> case 2:<br /> cout << \"2\"<br />GO<br /> break<br />GO<br /> default:<br /> cout << \"Unknown.\"<br />GO<br /> break<br />GO<br /><br /><br /><br />Just like with Java, failure to use the \"break;\" command in C will cause code to be run until one is reached.<br /><br />However, unlike Java, non-integer values, such as characters, can be used in a C/C++ switch-case statement. However, the values being tested for must be expressible as a C constant, so variables cannot be used.

    • 6

      Close with a bracket.<br /><br />switch(x) {<br /> case 1:<br /> cout << \"1\"<br />GO<br /> break<br />GO<br /> case 2:<br /> cout << \"2\"<br />GO<br /> break<br />GO<br /> default:<br /> cout << \"Unknown.\"<br />GO<br /> break<br />GO<br />}

    Pascal

    • 7

      Write the case statement:<br /><br />case x of<br /><br />You should notice that, compared to C and Java, Pascal replaces the \"switch\" keyword with the word \"case.\" It also replaces the \"default\" keyword with the word \"else.\"

    • 8

      Add the conditions. <br /><br />case x of<br /> 1: writeln('1')<br />GO<br /> 2: writeln('2')<br />GO<br /> else writeln('Unknown')<br />GO<br /><br />You should also notice that \"break;\" statements are unnecessary in Pascal.

    • 9

      Add the end statement. <br /><br />case x of<br /> 1: writeln('1')<br />GO<br /> 2: writeln('2')<br />GO<br /> else writeln('Unknown')<br />GO<br />end<br />GO<br /><br />Unlike Java and C, an \"end\" statement at the end of the whole block of code is needed rather than a closing bracket.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured