rss
Twitter Delicious Facebook Digg Stumbleupon Favorites

Sunday, October 9, 2011

Switch case

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C/C++, C#, Java, and so on. It is also included in several other types of languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch . The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

In his 1952 text Introduction to Metamathematics, Stephen Kleene formally proves that the CASE function (the IF-THEN-ELSE function being its simplest form) is a primitive recursive function, where he defines the notion definition by cases in the following manner:
"#F. The function φ defined thus
φ(x1 , ... , xn ) =
  • φ1(x1 , ... , xn ) if Q1(x1 , ... , xn ),
  • . . . . . . . . . . . .
  • φm(x1 , ... , xn ) if Qm(x1 , ... , xn ),
  • φm+1(x1 , ... , xn ) otherwise,
where Q1 , ... , Qm are mutually exclusive predicates (or φ(x1 , ... , xn) shall have the value given by the first clause which applies) is primitive recursive in φ1, ..., φm+1, Q1, ..., Qm+1. (Definition by cases)." (Kleene 1952:229)
Kleene provides a proof of this in terms of the Boolean-like recursive functions "sign-of" sg( ) and "not sign of" ~sg( ) (Kleene 1952:222-223); the first returns 1 if its input is positive and −1 if its input is negative.
Boolos-Burgess-Jeffrey make the additional observation that "definition by cases" must be both mutually exclusive and collectively exhaustive. They too offer a proof of the primitive recursiveness of this function (Boolos-Burgess-Jeffrey 2002:74-75).
The IF-THEN-ELSE is the basis of the McCarthy formalism – its usage replaces both primitive recursion and the mu-operator.

class SwitchCase{
      public static void main(String args[]){
          try{
          int num = Integer.parseInt(args[0]);
          int n = num;
          int reverse=0,remainder;
          while(num > 0){
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
           }
          String result="";
          while(reverse > 0){
               remainder = reverse % 10;
               reverse = reverse / 10;
               switch(remainder){
                    case 0 :
                             result = result + "Zero ";
                             break;
                    case 1 :
                             result = result + "One ";
                             break;
                    case 2 :
                             result = result + "Two ";
                             break;
                    case 3 :
                             result = result + "Three ";
                             break;
                    case 4 :
                             result = result + "Four ";
                             break;
                    case 5 :
                             result = result + "Five ";
                             break;
                    case 6 :
                             result = result + "Six ";
                             break;
                    case 7 :
                             result = result + "Seven ";
                             break;
                    case 8 :
                             result = result + "Eight ";
                             break;
                    case 9 :
                             result = result + "Nine ";
                             break;
                    default:
                             result="";
                 }
            }
                System.out.println(result);
        }catch(Exception e){
             System.out.println("Invalid Number Format");
         }
     }
}

2 comments:

fatah rohmani said...

SWITCH case can be used for long and complex conditional statement

Unknown said...

i like this slide

Post a Comment

Powered by Blogger.