Scanner class Methods in Java

Java-The switch Statement

The switch Statement



The switch statement is useful for programs that are written to check multiple conditions. It is mainly used to replace if-else-if ladder. The if-else-if ladder is complex and perplexing statement. The switch statement  is straight forward and simple. It can contain as many as conditions a programmer wants. It use cases to check conditions. It dose not need relational expressions.

Syntax: 

1. switch(expression)
2.{
3.  case constant1: statement(s) 1;
4.                               break;
5.   case constant2: statement(s) 2;
6.                              break;     
7.   .................................................
8.  case constantn: statement(s) n;
9.                               break;
10.  default: statement(s);
11.}

This syntax contains numbering for better understanding,
  1. The first line has the keyword switch to define switch statement , which is followed by (expression), in here we will write the expression whom we want to check.
  2. This line has a opening curly brace which contains the cases written in switch statement.
  3. The case keyword is used to define a case, which will check for the followed constant1 value in the expression. The constant can be any alphabet, number.Constant means whose value is fixed.The : is must here. After : , we can write desired statements. 
  4. The break is used to jump out of switch, never forget to write break after each case.If you do so , compiler will execute each case and print all statements.
  5. We can specify second case here.
  6. Second break statement after second case.
  7. This dots states we can have multiple cases.
  8. We can specify case for n  numbers.
  9. Each case must contain a break statement.
  10. The default keyword is used for default case. It can contain a default statement, which programmers writes to tell the expression does not match any case.
  11. The closing curly brace.
This was all about syntax of switch.Now, lets understand it using an example.

Example:   Prisha wants to write a program which can detect vowels using switch statement.The user will input any alphabet. Program will print vowel on detecting each vowel.

Solution:

As we know, in English we have five vowels  A,E,I,O,U in capitals and a,e,i,o,u in small letters. This program will check for both capital and small letters. All other letter are Consonant and default case will be used to print consonant on screen. There are 5 vowels and 21 consonants in English language.


program to check whether a given character is vowel or not using switch statement

Explanation: In the above example,
        1. First we declare a character variable named "ch"
        2. Created an object "input" of Scanner class, for getting input from user.
        3. Used next().charAt(0) method to read value of "ch".
        4. The switch statement will now, check for the value of" ch" which it read using Scanner method.
        5. The first case will check if  value of "ch"  is equal to 'a'.If yes then it will print Vowel! on screen using print statement and will jump out of switch due to break keyword else control will get transferred to next case.
        6. The step 5 goes on until the value of "ch " matched with any case.
        7. If "ch" value does not contain any vowel and does not match with any case. Then the default case will get executed and it will print Consonant! on Screen. See in  image below , it illustrates the case where user enter a Consonant value

program to check whether a given character is vowel or not using switch statement

Experiment: Try to make simple calculator using switch statement. Ask user to enter two numbers and print statement containing operations that can be performed like +, -, *, / . And ask used to enter any operator then using switch case perform operation on the numbers and print result on screen.

Try to create simple calculator yourself first, before jumping to the solution .

 
program to check whether a given character is vowel or not using switch statement

Try to  understand code by yourself and try other operators in output.

  Things to Remember while using switch

  1. The cases can be arranged in any order as programmer's choice.
  2. If you omit break statement, all cases will get executed  and all statements will get printed on screen. The break is a jump statement which helps the control to get out of switch when the particular case is met.
  3. A case label  can not contain a runtime expression involving variables or method calls like, (a+ab),(b+c()) are invalid cases.
  4. The floating point , boolean, and long types are not supported even though the long is an integer type.
  5. There is no need of brace in switch case. A case can contain multiple statements without the need of any brackets.It is  understood by compiler that all statements belong to a particular case.
  6. A case label can contain integer literals, character literals and enumeration constants.
  7. Omitting the space between the case keyword and case label can lead to a logical error.
  8. In single switch statement duplicate case labels are not allowed and also no duplicate default cases.
  9. If you don't use default case , the program will fall through the entire switch and continue to  next statement that follows the switch statement.The default case is used for exception handling.
  10. Switch statement can be nested. You can write inner switch in an outer switch statement.
      Keep Learning ! Happy Coding!                                         

Comments