Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Control flow
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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!
== Choice == === If-then-(else) statements === {{main article|Conditional (computer programming)}} Conditional expressions and conditional constructs are features of a [[programming language]] that perform different computations or actions depending on whether a programmer-specified [[Boolean data type|Boolean]] ''condition'' evaluates to true or false. * <code>IF..GOTO</code>. A form found in unstructured languages, mimicking a typical machine code instruction, would jump to (GOTO) a label or line number when the condition was met. * <code>IF..THEN..(ENDIF)</code>. Rather than being restricted to a jump, any simple statement, or nested block, could follow the THEN key keyword. This a structured form. * <code>IF..THEN..ELSE..(ENDIF)</code>. As above, but with a second action to be performed if the condition is false. This is one of the most common forms, with many variations. Some require a terminal <code>ENDIF</code>, others do not. [[C (programming language)|C]] and related languages do not require a terminal keyword, or a 'then', but do require parentheses around the condition. * Conditional statements can be and often are nested inside other conditional statements. Some languages allow <code>ELSE</code> and <code>IF</code> to be combined into <code>ELSEIF</code>, avoiding the need to have a series of <code>ENDIF</code> or other final statements at the end of a compound statement. {| class="wikitable" |- ! [[Pascal (programming language)|Pascal]]: ! [[Ada (programming language)|Ada]]: |- |<syntaxhighlight lang="pascal"> if a > 0 then writeln("yes") else writeln("no"); </syntaxhighlight> |<syntaxhighlight lang="ada"> if a > 0 then Put_Line("yes"); else Put_Line("no"); end if; </syntaxhighlight> |- ! [[C (programming language)|C]]: ! [[Shell script]]: |- |<syntaxhighlight lang="c"> if (a > 0) { puts("yes"); } else { puts("no"); } </syntaxhighlight> |<syntaxhighlight lang="bash"> if [ $a -gt 0 ]; then echo "yes" else echo "no" fi </syntaxhighlight> |- ! [[Python (programming language)|Python]]: ! [[Lisp (programming language)|Lisp]]: |- |<syntaxhighlight lang="python"> if a > 0: print("yes") else: print("no") </syntaxhighlight> |<syntaxhighlight lang="lisp"> (princ (if (plusp a) "yes" "no")) </syntaxhighlight> |} Less common variations include: * Some languages, such as early [[Fortran]],{{efn|In Fortran, this statement was deemed obsolescent in Fortran-90, and deleted as of Fortran 2018.}} have a ''three-way'' or ''[[arithmetic if]]'', testing whether a numeric value is negative, zero, or positive. *Some languages have a [[Functional programming|functional]] form of an <code>if</code> statement, for instance [[Lisp (programming language)|Lisp's]] <code>cond</code>. *Some languages have an [[Operator (programming)|operator]] form of an <code>if</code> statement, such as C's [[ternary operator]]. * [[Perl]] supplements a C-style <code>if</code> with <code>when</code> and <code>unless</code>. * [[Smalltalk]] uses <code>ifTrue</code> and <code>ifFalse</code> messages to implement conditionals, rather than any fundamental language construct. === Case and switch statements === {{Main article|Switch statement}} [[Switch statement]]s (or ''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 table]]s. In [[dynamic language]]s, 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 <code>*)</code> implements the default case as a [[glob (programming)|glob]] matching any string. Case logic can also be implemented in functional form, as in [[SQL]]'s <code>decode</code> statement.<!--Perl's implementation of case as a lookup table--> {| class="wikitable" |- ! [[Pascal (programming language)|Pascal]]: ! [[Ada (programming language)|Ada]]: |- |<syntaxhighlight lang="pascal"> case someChar of 'a': actionOnA; 'x': actionOnX; 'y','z':actionOnYandZ; else actionOnNoMatch; end; </syntaxhighlight> |<syntaxhighlight lang="ada"> case someChar is when 'a' => actionOnA; when 'x' => actionOnX; when 'y' | 'z' => actionOnYandZ; when others => actionOnNoMatch; end; </syntaxhighlight> |- ! [[C (programming language)|C]]: ! [[Shell script]]: |- |<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> |- ! [[Lisp (programming language)|Lisp]]: ! [[Fortran]]: |- | <syntaxhighlight lang="lisp"> (case some-char ((#\a) action-on-a) ((#\x) action-on-x) ((#\y #\z) action-on-y-and-z) (else action-on-no-match)) </syntaxhighlight> | <syntaxhighlight lang="fortran"> select case (someChar) case ('a') actionOnA case ('x') actionOnX case ('y','z') actionOnYandZ case default actionOnNoMatch end select </syntaxhighlight> |}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Control flow
(section)
Add topic