Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Conditional (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Case and switch statements == {{Main|Switch statement}} [[Switch statement]]s (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 [[Optimizing compiler|compiler optimizations]], such as [[lookup table]]s. 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. {| class="wikitable" |- ! [[Pascal (programming language)|Pascal]]: ! [[C (programming language)|C]]: ! [[Shell script]]: |- |<syntaxhighlight lang="pascal"> case someChar of 'a': actionOnA; 'x': actionOnX; 'y','z':actionOnYandZ; else actionOnNoMatch; end; </syntaxhighlight> |<syntaxhighlight lang="c"> switch (someChar) { case 'a': actionOnA; break; case 'x': actionOnX; break; case 'y': case 'z': actionOnYandZ; break; default: actionOnNoMatch; } </syntaxhighlight> |<syntaxhighlight lang="bash"> case $someChar in a) actionOnA; ;; x) actionOnX; ;; [yz]) actionOnYandZ; ;; *) actionOnNoMatch ;; esac </syntaxhighlight> |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)