Java Switch Statement
Details
- Details
- Category: Java
- Created on Wednesday, 07 November 2012 11:17
- Last Updated on Wednesday, 07 November 2012 11:30
- Published on Wednesday, 07 November 2012 11:17
- Written by Administrator
- Hits: 8446
Java Switch Statement :
package com.developerpages.snippets.basics;
public class SwitchStatementExample {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
switch (i) {
case 0:
System.out.println("case 0 , i = " + i);
break;
case 1:
System.out.println("case 1 , i = " + i);
break;
case 2:
System.out.println("case 2 , i = " + i);
break;
default:
System.out.println("i > 2");
}
}
}
}
output :
case 0 , i = 0
case 1 , i = 1
case 2 , i = 2
i > 2
i > 2
Free Flowing Switch Statement (without break statement):
package com.developerpages.snippets.basics;
public class FreeFlowingSwitchStatement {
public static void main(String[] args) {
int i = 0;
switch (i) {
case 0:
System.out.println("case 0 , i = " + i);
case 1:
System.out.println("case 1 , i = " + i);
case 2:
System.out.println("case 2 , i = " + i);
default:
System.out.println("Free flowing switch");
}
}
}
output:
case 0 , i = 0
case 1 , i = 0
case 2 , i = 0
Free flowing switch