Conditional (computer Programming) - Case and Switch Statements

Case and Switch Statements

Switch statements (in some languages, case statements or multiway branches) compare a given value with specified constants and take action according to the first constant to match. There is usually a provision for a default action ('else','otherwise') to be taken if no match succeeds. Switch statements can allow compiler optimizations, such as lookup tables. In dynamic languages, the cases may not be limited to constant expressions, and might extend to pattern matching, as in the shell script example on the right, where the '*)' implements the default case as a regular expression matching any string.

Pascal: C: Java: Shell script:
case someChar of 'a': actionOnA; 'x': actionOnX; 'y','z':actionOnYandZ; else actionOnNoMatch; end; switch (someChar) { case 'a': actionOnA; break; case 'x': actionOnX; break; case 'y': case 'z': actionOnYandZ; break; default: actionOnNoMatch; } switch (age) { case 1: System.out.printf("You're one."); break; case 2: System.out.printf("You're two."); break; case 3: System.out.printf("You're three."); break; case 4: System.out.printf("You're four."); break; default: System.out.printf("You're neither!"); break; } case $someChar in a) actionOnA; ;; x) actionOnX; ;; ) actionOnYandZ; ;; *) actionOnNoMatch ;; esac

Read more about this topic:  Conditional (computer Programming)

Famous quotes containing the words case, switch and/or statements:

    ... business training in early life should not be regarded solely as insurance against destitution in the case of an emergency. For from business experience women can gain, too, knowledge of the world and of human beings, which should be of immeasurable value to their marriage careers. Self-discipline, co-operation, adaptability, efficiency, economic management,—if she learns these in her business life she is liable for many less heartbreaks and disappointments in her married life.
    Hortense Odlum (1892–?)

    Uncritical semantics is the myth of a museum in which the exhibits are meanings and the words are labels. To switch languages is to change the labels.
    Willard Van Orman Quine (b. 1908)

    Nonwhite and working-class women, if they are ever to identify with the organized women’s movement, must see their own diverse experiences reflected in the practice and policy statements of these predominantly white middle-class groups.
    Kimberly Crenshaw (b. 1959)