1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/* Switch Statement Example This example shows how to use switch statement in a Java program. Switch statement is a better replacement if multiple if else if statements. */ public class SwitchStatementExample { public static void main(String[] args) { /* * Syntax of switch statement is * * switch(expression){ * * case value1: * //statements * break; * * case value2: * //statements * break; * * .... * * default: * //statements * break; * * } * * here, expression must be of type int, short, byte or char. * values should be constants literal values and can not be * duplicated. * * Flow of switch statement is as below. * Expression value is compared with each case value. If it * matches, statements following case would be executed. * break statement is used to terminate the execution of * statements. * * If none of the case matches, statements following default * would be executed. * * If break statement is not used within case, all cases following * matching cases would be executed. * */ for(int i=0; i <= 3 ; i++) { switch(i) { case 0: System.out.println("i is 0"); break; case 1: System.out.println("i is 1"); break; case 2: System.out.println("i is 2"); break; default: System.out.println("i is grater than 2"); } } } } /* Output would be i is 0 i is 1 i is 2 i is grater than 2 */ |
how we use this by using command line arguements
We can only use final variables. No other variable is accepted. We can also add expressions as the conditions but the expressions must be executable at compile time