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:

    I’m a very smart guy. I haven’t a feeling or a scruple in the world. All I have the itch for is money. I am so money greedy that for twenty-five bucks a day and expenses, mostly gasoline and whisky, I do my thinking myself, what there is of it; I risk my whole future, the hatred of the cops ... I dodge bullets and eat saps, and say thank you very much, if you have any more trouble, I hope you’ll think of me, I’ll just leave one of my cards in case anything comes up.
    Raymond Chandler (1888–1959)

    Children ... after a certain age do not welcome parental advice. Occasionally, they may listen to another adult, which is why perhaps people should switch children with their neighbors and friends for a while in the teen years!
    Marian Wright Edelman (20th century)

    The statements of science are hearsay, reports from a world outside the world we know. What the poet tells us has long been known to us all, and forgotten. His knowledge is of our world, the world we are both doomed and privileged to live in, and it is a knowledge of ourselves, of the human condition, the human predicament.
    John Hall Wheelock (1886–1978)