Forum

Number Of Page View:100,000+/Months
LOGIN |New User

Reply

Writing Code Using Switch Statement
Many of the ways are the simulate the values of the use of the multiple if statements is with the switch statement.
int x = 3;
if(x == 1) {
System.out.println("x equals 1");
}
else if(x == 2) {
System.out.println("x equals 2");
}
else if(x == 3) {
System.out.println("x equals 3");
}
else {
System.out.println("No idea what x is");
}

--------------------------------------------------
Illegal Arguments to if  | Legal Arguments to if
--------------------------------------------------
int x = 1;               | int x = 1;
if (x) { }                 if (x == 1) { }
--------------------------------------------------
if (0) { }               | if (false)
--------------------------------------------------
if (x = 6)               | if (x == 6)
--------------------------------------------------
            Here same functionality represented in a switch construct : 
int x = 3;
switch (x) {
case 1:
System.out.println("x is equal to 1");
break;
case 2:
System.out.println("x is equal to 2");
break;
case 3:
System.out.println("x is equal to 3");
break;
default:
System.out.println("Still no idea what x is");
} 
Posted By: Name:Rajesh Kr URL: Writing Code Using Switch Statement