GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Programs/loopAndFlow/SwitchExamples.java at master · divScorp/Java-Programs Expand file tree/ Copy pathSwitchExamples.javaMore file actions 108 lines (100 loc) · 2.22 KB / Copy pathSwitchExamples.javaFile metadata and controls108 lines (100 loc) · 2.22 KB 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 package loopAndFlow; public class SwitchExamples { public static void main(String[] args) { int number = 2; // Output of below switch is 2. // The case which is matched is executed. switch (number) { case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; default: System.out.println("Default"); break; } // Notice that there is not break after each case. // If there is no break, then all the case's until we find break are // executed. number = 2; switch (number) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); default: System.out.println("Default"); } // Output of above switch is 2 3 Default on separate lines. // Notice that there is not break after case 2. So, it fall through // to case 3. Prints the statement. After that, the break statement // takes the execution out of the switch. number = 2; switch (number) { case 1: System.out.println(1); break; case 2: case 3: System.out.println("Number is 2 or 3"); break; default: System.out.println("Default"); break; } // Output of above switch is Number is 2 or 3. // default is executed if none of the case's match number = 10; switch (number) { case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; default: System.out.println("Default"); break; } // Output of above is Default // default doesn't need to be the last case in an switch. number = 10; switch (number) { default: System.out.println("Default"); break; case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; } // Output of above is Default // No change in Result // Switch can be used only with char, byte, short, int or enum //long l = 15; /* * switch(l){//COMPILER ERROR. Not allowed. } */ // Case value should be a compile time constant. number = 10; switch (number) { // case number>5://COMPILER ERROR. Cannot have a condition // case number://COMPILER ERROR. Should be constant. } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |