Welcome back to my blog!!! In my previous post you learned about if, nested ifs and else-if-else in Java. In this post you will learn about switch.
What is a switch? In Java, a switch is a multiway branch statement.
General form of a switch:
switch(expression) {
case value1: statement;
break;
case value2: statement;
break;
.
.
.
.
.
.
default: statement;
break;
}
The expression must be the type of int, byte, short or char. Duplicate case values are not allowed.
The switch statement works like this: the value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following the case statement is executed. If none of the constants matches the value of the expressions, then the default statement is executed. however, the default statement is optional. If no case matches and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branch is to the first line of the code that follows the entire switch statement. This has the effect of "jumping out" of the switch. Given below is a simple example of switch:
class switch {
public static void main(String[] args) {
for(int i = 0; i<6; i++)
{
case 0: System.out.println(" i is zero");
break;
case 1: System.out.println("i is one");
break;
case 2: System.out.println("i is two");
break;
case 3: System.out.println("i is three");
break;
default: System.out.println("i is greater than 3");
break;
}
}
}
What is a switch? In Java, a switch is a multiway branch statement.
General form of a switch:
switch(expression) {
case value1: statement;
break;
case value2: statement;
break;
.
.
.
.
.
.
default: statement;
break;
}
The expression must be the type of int, byte, short or char. Duplicate case values are not allowed.
The switch statement works like this: the value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following the case statement is executed. If none of the constants matches the value of the expressions, then the default statement is executed. however, the default statement is optional. If no case matches and no default is present, then no further action is taken.
The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branch is to the first line of the code that follows the entire switch statement. This has the effect of "jumping out" of the switch. Given below is a simple example of switch:
class switch {
public static void main(String[] args) {
for(int i = 0; i<6; i++)
{
case 0: System.out.println(" i is zero");
break;
case 1: System.out.println("i is one");
break;
case 2: System.out.println("i is two");
break;
case 3: System.out.println("i is three");
break;
default: System.out.println("i is greater than 3");
break;
}
}
}
Nested switch statements:
You can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and in the outer switch. For example, the following fragment is perfectly valid:
switch(count) {
case 1:
switch(target) {
case 0:
System.out.println("Target is zero");
break;
case 1:
System.out.println("Target is one");
break;
}
break;
case 2: . . . . . .
}
Thank you for coming to my blog. Now you have learned about switch in Java. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
Comments
Post a Comment