- Get link
- X
- Other Apps
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.{
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,
- 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.
- This line has a opening curly brace which contains the cases written in switch statement.
- 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.
- 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.
- We can specify second case here.
- Second break statement after second case.
- This dots states we can have multiple cases.
- We can specify case for n numbers.
- Each case must contain a break statement.
- 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.
- The closing curly brace.
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.
Explanation: In the above example,
- First we declare a character variable named "ch"
- Created an object "input" of Scanner class, for getting input from user.
- Used next().charAt(0) method to read value of "ch".
- The switch statement will now, check for the value of" ch" which it read using Scanner method.
- 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.
- The step 5 goes on until the value of "ch " matched with any case.
- 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
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 .
Try to understand code by yourself and try other operators in output.
Things to Remember while using switch
- The cases can be arranged in any order as programmer's choice.
- 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.
- A case label can not contain a runtime expression involving variables or method calls like, (a+ab),(b+c()) are invalid cases.
- The floating point , boolean, and long types are not supported even though the long is an integer type.
- 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.
- A case label can contain integer literals, character literals and enumeration constants.
- Omitting the space between the case keyword and case label can lead to a logical error.
- In single switch statement duplicate case labels are not allowed and also no duplicate default cases.
- 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.
- Switch statement can be nested. You can write inner switch in an outer switch statement.
Keep Learning ! Happy Coding!
Comments
Post a Comment
If you have any doubt, ask here