SWITCH STATEMENTS
Switch statements
are another improvised form of conditional statements inside Java, unlike
if-else they allow us to act according to specific inputs rather then just
checking conditions. So let's jump into the implementation and have a look at
the basic syntax of a switch statements.
Syntax:
switch (input) {
case input 1: //code
break;
case input 2:
//code
break;
case input 3: //code
break;
default: //code
}
So we first write
the keyword switch and then the variable name (within the parentheses) with
whose value we have to play, and the rest of the code within the curly braces
after that. Now inside the curly brackets we can use the keyword case
followed by the the actual input value,
and after the input we can write a colon followed by the code which we
will be executing if the input matches the one in that case. Also the break statements are used to execute
the code just after the case with which the input matches and then break out
from the rest of the statements below it.
So let's look at an
example for a better understanding
public class Coding_Winds {
public static void main(String[] args) {
Scanner
sc = new Scanner(System.in);
System.out.println("Enter
any day of the week");
String
day = sc.nextLine();
switch (day){
case "monday":System.out.println("You have entered the first day of the week");
break;
case "tuesday": System.out.println("You have enterd the first day of the week");
break;
case "wednesday": System.out.println("You are in the third day of the week");
break;
case "thursday": System.out.println("You are in the fourth day of the week ");
break;
case "friday" : System.out.println("You are in the fifth day of the week");
break;
case "saturday" : System.out.println("You are in the weekend");
break;
case "sunday": System.out.println("Ahh shit tommorrow is Monday again");
break;
default: System.out.println("Invalid Input!");
}
}
}
Output:
Now if you will enter anything apart from the weekdays the
default print statement will be ‘Invalid Input!’, but apart from that even if
your entered string will be not exactly match the string after the case keyword
, then also we’ll get the default print statement. For example if the user will
enter “Monday” or “MONDAY” instead of “monday”, the default statement will be
executed (i.e., ‘Invalid Input!’) . So in order to make our code more
acceptable we can customize our code in the following manner
case "monday":
case "MONDAY":
case "Monday":
System.out.println("You have entered the first
day of the week");
break;
and we can do this with all our cases, so when the user will
input either of the three inputs (i.e., “MONDAY”, “Monday” or “monday”) we will
be getting the same output. But then again we can use the string function to
convert all the alphabets in our string to lowercase (toLowerCase()) and then
leave our code as it was initially
switch (day.toLowerCase()){
case "monday":System.out.println("You have entered the first
day of the week");
break;
case "tuesday": System.out.println("You have enterd the first
day of the week");
break;
case "wednesday": System.out.println("You are in the third day of
the week");
break;
case "thursday": System.out.println("You are in the fourth day of
the week ");
break;
case "friday" : System.out.println("You are in the fifth day of
the week");
break;
case "saturday" : System.out.println("You are in the weekend");
break;
case "sunday": System.out.println("Ahh shit tommorrow is Monday
again");
break;
default: System.out.println("Invalid Input!");
}
}
}
And now if we will enter even “MoNdAy” as our input, we will
get our desired output (the print statement in case “monday”).
Remember, if we are using integers as input then we will
obviously not put them in double quotes, and if our input is a character then we will wrap them in single quotes.
Ex:
public class Coding_Winds {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter any alphabet");
char day = sc.next().charAt(0);
switch (day){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
System.out.println("It's a vowel");
break;
default: System.out.println("It's a consonant");
}
}
}
Hope you are clear on this topic do read our more articles on JAVA LANGUAGE.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
This article is SUBMITTED By : Pranjal Rai
Do subscribe to our daily blog update by clicking here.
Thank You!