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
Control flow
(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!
=== 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.
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)