Tuesday, October 14, 2008

Java - If / Else vs. Switch

Both of these produce the exact same results:
if (opt == 1)
{
funcInst.func1();
}
else if (opt == 2)
{
funcInst.func2();
}
else
{
System.out.print("Invalid Option");
}
switch(opt)
{
case 1 : funcInst.func1() ;
break;
case 2 : funcInst.func2() ;
break;
default : System.out.print("Invalid Option");
break;
}


No comments:

Post a Comment