switch statement in java
Switch statement in java:
This statement is used to execute one statement among the different statements. In other words, the switch statement is a multiple branched statement similar to if-else-if ladder statement. In switch statement, based on the value it will check with different parts of the code and dispatch the statement. The value of expression can be int,byte,short,char primitive data types and it also accept the enumerated types, wrapper class and string object
Important points:
Syntax:
switch(expression)
{
case 1:
//Statement
break;
case 2:
//Statement
break;
case 3:
//Statement
break;
.
.
.
.
case n:
//Statement
break;
default:
// default statement
}
Example:
Without break statement:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
switch(3)
{
case 1:
System.out.println("Statement 1 ");
case 2:
System.out.println("Statement 2 ");
case 3:
System.out.println("Statement 3 ");
case 4:
System.out.println("Statement 4 ");
case 5:
System.out.println("Statement 5 ");
default:
System.out.println("Statement default ");
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Statement 3
Statement 4
Statement 5
Statement default
end of switch statement
with break statement:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
int i = 3;
switch(i)
{
case 1:
System.out.println("Statement 1 ");
break;
case 2:
System.out.println("Statement 2 ");
break;
case 3:
System.out.println("Statement 3 ");
break;
case 4:
System.out.println("Statement 4 ");
break;
case 5:
System.out.println("Statement 5 ");
break;
default:
System.out.println("Statement default ");
break;
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Statement 3
end of switch statement
Switch statement with enumerated datatypes:
enum days
{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;
}
public class Enumtesting
{
days d;
Enumtesting(days d)
{
this.d = d;
}
public void printSwitch()
{
switch(d)
{
case Sunday:
System.out.println("Sunday");
break;
case Monday:
System.out.println("Monday");
break;
case Tuesday:
System.out.println("Tuesday");
break;
case Wednesday:
System.out.println("Wednesday");
break;
case Thursday:
System.out.println("Thursday");
break;
case Friday:
System.out.println("Friday");
break;
case Saturday:
System.out.println("Saturday");
break;
default:
System.out.println("Statement default ");
break;
}
}
public static void main(String[] args)
{
System.out.println("Switch statement begins");
Enumtesting a = new Enumtesting(days.Sunday);
a.printSwitch();
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Sunday
end of switch statement
Switch statement with string object:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
String s = "Monday";
switch(s)
{
case "Sunday":
System.out.println("Sunday");
break;
case "Monday":
System.out.println("Monday");
break;
case "Tuesday":
System.out.println("Tuesday");
break;
case "Wednesday":
System.out.println("Wednesday");
break;
case "Thursday":
System.out.println("Thursday");
break;
case "Friday":
System.out.println("Friday");
break;
case "Saturday":
System.out.println("Saturday");
break;
default:
System.out.println("Statement default ");
break;
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Monday
end of switch statement
This statement is used to execute one statement among the different statements. In other words, the switch statement is a multiple branched statement similar to if-else-if ladder statement. In switch statement, based on the value it will check with different parts of the code and dispatch the statement. The value of expression can be int,byte,short,char primitive data types and it also accept the enumerated types, wrapper class and string object
Important points:
- The value for the case must same datatypes as the variable in the switch.
- The value for the case should constants or literals. it cannot be a variable.
- The value for the case with duplicates is not allowed.
- The break statement at the end of the each and every case is not mandatory. It is optional.
- The break statement is used to terminate statement sequence without executing the next case.
- The default statement in the switch is not mandatory.
- The case without the break statement continues to next case.
Switch statement in java |
Syntax:
switch(expression)
{
case 1:
//Statement
break;
case 2:
//Statement
break;
case 3:
//Statement
break;
.
.
.
.
case n:
//Statement
break;
default:
// default statement
}
Example:
Without break statement:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
switch(3)
{
case 1:
System.out.println("Statement 1 ");
case 2:
System.out.println("Statement 2 ");
case 3:
System.out.println("Statement 3 ");
case 4:
System.out.println("Statement 4 ");
case 5:
System.out.println("Statement 5 ");
default:
System.out.println("Statement default ");
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Statement 3
Statement 4
Statement 5
Statement default
end of switch statement
with break statement:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
int i = 3;
switch(i)
{
case 1:
System.out.println("Statement 1 ");
break;
case 2:
System.out.println("Statement 2 ");
break;
case 3:
System.out.println("Statement 3 ");
break;
case 4:
System.out.println("Statement 4 ");
break;
case 5:
System.out.println("Statement 5 ");
break;
default:
System.out.println("Statement default ");
break;
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Statement 3
end of switch statement
Switch statement with enumerated datatypes:
enum days
{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;
}
public class Enumtesting
{
days d;
Enumtesting(days d)
{
this.d = d;
}
public void printSwitch()
{
switch(d)
{
case Sunday:
System.out.println("Sunday");
break;
case Monday:
System.out.println("Monday");
break;
case Tuesday:
System.out.println("Tuesday");
break;
case Wednesday:
System.out.println("Wednesday");
break;
case Thursday:
System.out.println("Thursday");
break;
case Friday:
System.out.println("Friday");
break;
case Saturday:
System.out.println("Saturday");
break;
default:
System.out.println("Statement default ");
break;
}
}
public static void main(String[] args)
{
System.out.println("Switch statement begins");
Enumtesting a = new Enumtesting(days.Sunday);
a.printSwitch();
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Sunday
end of switch statement
Switch statement with string object:
public class Example
{
public static void main(String[] args)
{
System.out.println("Switch statement begins");
String s = "Monday";
switch(s)
{
case "Sunday":
System.out.println("Sunday");
break;
case "Monday":
System.out.println("Monday");
break;
case "Tuesday":
System.out.println("Tuesday");
break;
case "Wednesday":
System.out.println("Wednesday");
break;
case "Thursday":
System.out.println("Thursday");
break;
case "Friday":
System.out.println("Friday");
break;
case "Saturday":
System.out.println("Saturday");
break;
default:
System.out.println("Statement default ");
break;
}
System.out.println("end of switch statement");
}
}
Output:
Switch statement begins
Monday
end of switch statement
Comments
Post a Comment