int value;
Now, suppose this int can have up to 5 values associated with it. Let’s say {0, 1, 2, 3, 4}.
For each value, we need to do different processing.
So, for example, take a look below :
Requirement :
-> for value 0 : call a function ‘processValueZero()’
-> for value 1 : call a function ‘processValueOne() and processValueTwo()’
-> for value 2 : call a function ‘processValueTwo()’
-> for value 3 : call a function ‘processValueThree()’
-> for value 4 : call a function ‘processValueFour()’
-> for all other values, run a default function.
So friends, what’s the first thought you have in your mind to solve this problem.
If you are thinking If-else statements, you are probably correct! We can achieve our desired behavior through that, but there is still. The more elegant solution so that we won’t have to write the statements at all!
Think for 2 mins here!
Yes! The answer is ‘switch statements’! Bravo! For those who guessed it correctly! Now, for those who don’t know what they are, no worries! We will understand it together. So, what’s the wait? let’s start!
What are switch statements?
Taking the above example, we will create a switch statement for it and see how magical it is! :p
Code:
import java.util.Scanner;
public class Switch {
static Scanner sc;
public static void main(String[] args) {
int value;
sc = new Scanner(System.in);
value = providesSomeValue();
switch (value) {
case 0:
System.out.println("value is 0 !!!");
processValueZero();
break;
case 1:
System.out.println("value is 1 !!!");
processValueOne();
case 2:
System.out.println("value is 2 !!!");
processValueTwo();
break;
case 3:
System.out.println("value is 3 !!!");
processValueThree();
break;
case 4:
System.out.println("value is 4 !!!");
processValueFour();
break;
default:
System.out.println("default case !!!");
processUndefinedValue();
break;
}
}
private static void processUndefinedValue() {
}
private static void processValueFour() {
}
private static void processValueThree() {
}
private static void processValueTwo() {
}
private static void processValueOne() {
}
private static void processValueZero() {
}
private static int providesSomeValue() {
return sc.nextInt();
}
}
Did you guys observe anything? Take some time and study this code to understand what has been done.
Think and understand the code snippet above for 2 minutes and note down some intriguing points that you have in mind.
Points to be noted:
The switch statement has to be given a 'value' through which, it will handle to charge to a ‘case’ accordingly. For example, let’s suppose the value is 0. (Before reading further, make a flow diagram of how the calls will proceed further on your own)
The call comes to switch statements.
I.e.
Now, as the value is 0, the call goes to the associated ‘case’. Here, in his situation, the call will go to the ‘case 0 ’ and, the output will be “value is 0 !!!” And the ‘processValueZero()’ function will be executed.
Intriguing question:
Now similarly, can you guys tell me what will happen in case of value is ‘1’?
Think for 2 minutes and then we will discuss.
Now, if the value is ‘1’, the switch statement will delegate the call to ‘case [value]’, here, in our situation which is ‘case 1’. So, the output will be “value is 1 !!!” And the function ‘processValueOne()’. But, is that it?
The answer is ‘NO’.
Solution:
The final output will be :
“Value is 1” and function ‘processValueOne()’ will be executed along with the output
“Value is 2” and function ‘processValueTwo()’ will be executed.
Can you guys tell me why?
Yes! Because of the ‘break’ keyword!
What is Break?
The break keyword will break the execution from that point. But, in case 1, there is no break keyword at the end. So, the execution will not break and the call will go to case 2 too!!
Carefully see the code again, to our requirement, isn’t this what we wanted? To execute one and two both!
Exactly!
A noteworthy point is also the default case. Whenever a value is received for which, no associated case is present, that will be handled by the ‘default case’. To take an example,
If the value received is ‘-1’ or ‘6’, we have no associated case for it, so the call will be handled by default case.
Also, points to be noted :
-> Almost in all cases, the switch has a better efficiency compared to if-else if-else statements, as the 'if-else statement works line by line, whereas the 'switch' statement gives the control to the associated case directly.
-> not only Integer but, characters can also be used as values in a switch. (From java 17, regular expressions can also be used in switch)
So guys, hope you enjoyed the journey ‘switch’. Will keep you guys updated on similar interesting topics and talks.
Till then, keep switching!! :p
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.