In Java, when we handles multiple conditions that time we can use either multiple if-else statements or we can use switch statements also.
When a variable can have multiple values and for different values it will executes different statements, in that case switch statements. A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Syntax –
switch(expression){ case label-1: statements; break; case label-2: statements; break; default: statements; } |
- this expression can be any integer or character variable.
- label-n -> constant integer/character.(n=0,1,2,…)
- default block is not mandatory.
- if ‘expression’ in switch statement get evaluated, then other case blocks will gets ready for execution. If the required value finds into cases then that block will executed.
- if any case block does not executed, then default block will executed.
- after every case block or even default block always end with ‘break’ statement. The break statement to end processing of a particular labeled statement within the switch statement.
In the label-n we can’t use any relational, logical operators. We will discuss about this below..
let’s understand through below code example…
public class switch1 { public static void main(String[] args) { int a=10; switch (a) { case 5: System.out.println(“a is 5”); break; case 10: System.out.println(“a is 10”); break; } } } |

from the upper code, integer variable ‘a’ value ‘10’ get matched.
Let’s understand through another example
public class switch2 { public static void main(String[] args) { char gen; gen = ‘m’; switch (gen) { case ‘f’: System.out.println(“female”); break; case ‘o’: System.out.println(“trans-gender”); break; case ‘m’: System.out.println(“male”); break; } } } |

from the upper code, you see we have stored character variable ‘gen’ values as ‘m’. which matches third case block as male.
Let’s understand uses of default block. As previous we know, when any cases not satisfies then default block get executed.
public class switch3 { public static void main(String[] args) { int c=50; switch (c) { case 10: System.out.println(“c is 10”); break; case 20: System.out.println(“c is 20”); break; default: System.out.println(“default block executed!!”); break; } } } |

from the upper code, you see integer variable ‘c’ stored value ‘50’. which does not matches any case-block, that’s why default block executed.
* remember: any relational operators(> < >= <= ==) along with logical operators(AND OR NOT) also not allowed into case block value(constant variable) place.
Let’s understand through below code…
public class switch4 { public static void main(String[] args) { int b=25; int c=b; switch (b) { case 20: System.out.println(“b is 20”); break; case 21: System.out.println(“b is 21”); break; case c==25: System.out.println(“b is 25”); break; } } } |

let’s understand another example…
public class switch5 { public static void main(String[] args) { int g=40; switch (g) { case g>20 &&g<30: System.out.println(“g between 20 and 30”); break; case g>30 &&g<50: System.out.println(“g between 30 and 50”); break; } } } |

let’s understand what’s the advantage to use switch cases over if-else ladder.
Let’s make a code, which tells grades according to marks. Conditions are –
>90 excellent
70-90 very good
60-70 good
50-60 moderate
<50 need to improve.
This problem can be solved using either if-else or using switch cases. Both the cases output goes same. Let’s see the code below…
// check grades using if-else ladder import java.util.Scanner; public class ifElse { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println(“Enter mark(0-100): “); int n=sc.nextInt(); if (n>90) { System.out.println(“excellent”); } if (n>70 &&n<90) { System.out.println(“very good”); } if (n>60 &&n<70) { System.out.println(“good”); } if (n>50 &&n<60) { System.out.println(“moderate”); } if (n<50) { System.out.println(“need to improve”); } } } |

// check grades using switch statement import java.util.Scanner; public class switch6 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println(“Enter mark(0-100): “); int n=sc.nextInt(); n=(n/10); switch (n) { case 10: System.out.println(“excellent”); break; case 9: System.out.println(“excellent”); break; case 8: System.out.println(“very good”); break; case 7: System.out.println(“very good”); break; case 6: System.out.println(“good”); break; case 5: System.out.println(“moderate”); break; case 4: System.out.println(“need to imrpove”); break; default: System.out.println(“need to imrpove”); break; } } } |
