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
C syntax
(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!
===Selection statements=== C has two types of [[selection statement]]s: the [[If statement|{{code|if}} statement]] and the [[Switch statement|{{code|switch}} statement]]. The {{code|if}} statement is in the form: <syntaxhighlight lang=C> if (<expression>) <statement1> else <statement2> </syntaxhighlight> In the {{code|if}} statement, if the {{code|<expression>}} in parentheses is nonzero (true), control passes to {{code|<statement1>}}. If the {{code|else}} clause is present and the {{code|<expression>}} is zero (false), control will pass to {{code|<statement2>}}. The {{code|else <statement2>}} part is optional and, if absent, a false {{code|<expression>}} will simply result in skipping over the {{code|<statement1>}}. An {{code|else}} always matches the nearest previous unmatched {{code|if}}; braces may be used to override this when necessary, or for clarity. The {{code|switch}} statement causes control to be transferred to one of several statements depending on the value of an [[Expression (mathematics)|expression]], which must have [[integral type]]. The substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more {{code|case}} labels, which consist of the keyword {{code|case}} followed by a constant expression and then a colon (:). The syntax is as follows: <syntaxhighlight lang=C> switch (<expression>) { case <label1> : <statements 1> case <label2> : <statements 2> break; default : <statements 3> } </syntaxhighlight> No two of the case constants associated with the same switch may have the same value. There may be at most one {{code|default}} label associated with a switch. If none of the case labels are equal to the expression in the parentheses following {{code|switch}}, control passes to the {{code|default}} label or, if there is no {{code|default}} label, execution resumes just beyond the entire construct. Switches may be nested; a {{code|case}} or {{code|default}} label is associated with the innermost {{code|switch}} that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a {{code|break;}} statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if {{code|<label2>}} is reached, the statements {{code|<statements 2>}} are executed and nothing more inside the braces. However, if {{code|<label1>}} is reached, both {{code|<statements 1>}} and {{code|<statements 2>}} are executed since there is no {{code|break}} to separate the two case statements. It is possible, although unusual, to insert the {{code|switch}} labels into the sub-blocks of other control structures. Examples of this include [[Duff's device]] and [[Simon Tatham]]'s implementation of [[coroutine]]s in [[Putty (SSH)|Putty]].<ref>{{cite web | first=Simon | last=Tatham | author-link=Simon Tatham | date=2000 | url=https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html | title = Coroutines in C| access-date=2017-04-30}}</ref>
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)