Switch statement (Part 6)
The Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case , and the variable being switched on is checked for each case.
Syntax:-
switch(expression) { case
value
:
// Statement
s break;
// optional
case
value
:
// Statements break; // optional // You can have any number of case statements. default : // Optional // Statements
}
Flowchart:-
Eg:-
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner scan = new Scanner (System.in);
System.out.println("Enter a value: ");
int day=scan.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
case 4:
System.out.println("Thursday");
case 5:
System.out.println("Friday");
case 6:
System.out.println("Saturday");
case 7:
System.out.println("Sunday");
}
scan.close();
}
}
Output:-
Enter a value:
4
Thursday
Friday
Saturday
Sunday
In the above program , the value entered corresponds to the week day and prints out next week days .
But what if we want to enter the same value 4 and get output as only Thursday .
Here is where break and continue statements come into action.
Break statement
When Java reaches a break keyword, it breaks out of loop , be it any .
This will stop the execution of more code and case testing inside the block.
We will apply break statement into the above program to get the desired result:
Eg:-
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner scan = new Scanner (System.in);
System.out.println("Enter a value: ");
int day=scan.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
scan.close();
}
}
Output:-
Enter a value:
4
Thursday
Continue statements
Whenever it is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration , skipping the execution of statements inside loop’s body for the current iteration.
This is particularly useful when you want to continue the loop but do not want the rest of the statements(after continue statement) in loop body to execute for that particular iteration.
You cannot use continue in a switch statement because continue is for jumping back to the beginning of a loop, but you don't have any loop in that code.
Below is an example of statement in a for loop , but you can use it in any of the loops without Switch statement.
Eg:-
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value: ");
int num = scan.nextInt();
System.out.println("The output is: ");
for (int i=1;i<=num;i++)
{
if (i==5)
{
continue; //this means that number 5 would not display in the output
}
System.out.println(i);
}
Output:-
Enter a value:
10
The output is:
1
2
3
4
6
7
8
9
10
Here value 5 is not displayed as the continue statement skips the iteration.