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!
== If–then(–else) == {{Redirect|if-then-else|the album|If Then Else|the TV episode|If-Then-Else}} The <code>if–then</code> or <code>if–then–else</code> construction is used in many programming languages. Although the syntax varies from language to language, the basic structure (in [[pseudocode]] form) looks like this: '''If''' (Boolean condition) '''Then''' (consequent) '''Else''' (alternative) '''End If''' For example: '''If''' stock=0 '''Then''' message= order new stock '''Else''' message= there is stock '''End If''' In the example code above, the part represented by ''(Boolean condition)'' constitutes a conditional ''expression'', having intrinsic value (e.g., it may be substituted by either of the values <code>True</code> or <code>False</code>) but having no intrinsic meaning. In contrast, the combination of this expression, the <code>If</code> and <code>Then</code> surrounding it, and the consequent that follows afterward constitute a conditional ''statement'', having intrinsic meaning (e.g., expressing a coherent logical rule) but no intrinsic value. When an [[interpreter (computing)|interpreter]] finds an <code>If</code>, it expects a [[Boolean data type|Boolean]] condition – for example, <code>x > 0</code>, which means "the variable x contains a number that is greater than zero" – and evaluates that condition. If the condition is <code>true</code>, the statements following the <code>then</code> are executed. Otherwise, the execution continues in the following branch – either in the <code>else</code> [[Block (programming)|block]] (which is usually optional), or if there is no <code>else</code> branch, then after the <code>end If</code>. After either branch has been executed, [[control flow|control]] returns to the point after the <code>end If</code>. === History and development === In early programming languages, especially some dialects of [[BASIC]] in the 1980s [[home computer]]s, an <code>if–then</code> statement could only contain <code>[[GOTO]]</code> statements (equivalent to a [[Branch (computer science)|branch]] instruction). This led to a hard-to-read style of programming known as [[spaghetti programming]], with programs in this style called ''spaghetti code''. As a result, [[structured programming]], which allows (virtually) arbitrary statements to be put in statement blocks inside an <code>if</code> statement, gained in popularity, until it became the norm even in most BASIC programming circles. Such mechanisms and principles were based on the older but more advanced [[ALGOL]] family of languages, and ALGOL-like languages such as [[Pascal (programming language)|Pascal]] and [[Modula-2]] influenced modern BASIC variants for many years. While it is possible while using only <code>GOTO</code> statements in <code>if–then</code> statements to write programs that are not spaghetti code and are just as well structured and readable as programs written in a structured programming language, structured programming makes this easier and enforces it. Structured <code>if–then–else</code> statements like the example above are one of the key elements of structured programming, and they are present in most popular high-level programming languages such as [[C (programming language)|C]], [[Java (programming language)|Java]], [[JavaScript]] and [[Visual Basic]] . ==== The "dangling else" problem ==== {{Main|Dangling else}} The <code>else</code> keyword is made to target a specific <code>if–then</code> statement preceding it, but for [[Nesting (computing)|nested]] <code>if–then</code> statements, classic programming languages such as [[ALGOL 60]] struggled to define which specific statement to target. Without clear boundaries for which statement is which, an <code>else</code> keyword could target any preceding <code>if–then</code> statement in the nest, as parsed. '''if''' a '''then''' '''if''' b '''then''' s '''else''' s2 can be parsed as '''if''' a '''then''' ('''if''' b '''then''' s) '''else''' s2 or '''if''' a '''then''' ('''if''' b '''then''' s '''else''' s2) depending on whether the <code>else</code> is associated with the first <code>if</code> or second <code>if</code>. This is known as the [[dangling else]] problem, and is resolved in various ways, depending on the language (commonly via the <code>end if</code> statement or <code>{...}</code> brackets). === Else if=== By using <code>else if</code>, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. '''if''' condition '''then''' ''-- statements'' '''elseif''' condition '''then''' ''-- more statements'' '''elseif''' condition '''then''' ''-- more statements;'' ... '''else''' ''-- other statements;'' '''end if'''; For example, for a shop offering as much as a 30% discount for an item: '''if''' discount < 11% '''then''' print (you have to pay $30) '''elseif''' discount<21% '''then''' print (you have to pay $20) '''elseif''' discount<31% '''then''' print (you have to pay $10) '''end if'''; In the example above, if the discount is 10%, then the first if statement will be evaluated as true and "you have to pay $30" will be printed out. All other statements below that first if statement will be skipped. The <code>elseif</code> statement, in the [[Ada (programming language)|Ada]] language for example, is simply [[syntactic sugar]] for <code>else</code> followed by <code>if</code>. In Ada, the difference is that only one <code>end if</code> is needed, if one uses <code>elseif</code> instead of <code>else</code> followed by <code>if</code>. [[PHP]] uses the <code>elseif</code> keyword<ref name="php_elseif">''[http://php.net/manual/control-structures.elseif.php PHP elseif syntax]''</ref> both for its curly brackets or colon syntaxes. [[Perl]] and [[Ruby (programming language)|Ruby]] provide the keyword <code>elsif</code> to avoid the large number of braces that would be required by multiple <code>if</code> and <code>else</code> statements. [[Python (programming language)|Python]] uses the special keyword <code>elif</code> because structure is denoted by indentation rather than braces, so a repeated use of <code>else</code> and <code>if</code> would require increased indentation after every condition. Some implementations of [[BASIC]], such as [[Visual Basic]],<ref name="vb_elseif">''[https://docs.microsoft.com/dotnet/visual-basic/language-reference/statements/if-then-else-statement Visual Basic ElseIf syntax]''</ref> use <code>ElseIf</code> too. Similarly, the earlier UNIX shells (later gathered up to the POSIX shell syntax<ref name="posixshell">''[http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html POSIX standard shell syntax]''</ref>) use elif too, but giving the choice of delimiting with spaces, line breaks, or both. However, in many languages more directly descended from Algol, such as [[Simula (programming language)|Simula]], [[Pascal (programming language)|Pascal]], [[BCPL (programming language)|BCPL]] and [[C (programming language)|C]], this special syntax for the <code>else if</code> construct is not present, nor is it present in the many syntactical derivatives of C, such as [[Java (programming language)|Java]], [[ECMAScript]], and so on. This works because in these languages, any ''single'' statement (in this case <code>if ''cond''</code>...) can follow a conditional without being enclosed in a block. This design choice has a slight "cost". Each <code>else if</code> branch effectively adds an extra nesting level. This complicates the job for the compiler (or the people who write the compiler), because the compiler must analyse and implement arbitrarily long <code>else if</code> chains recursively. If all terms in the sequence of conditionals are testing the value of a single expression (e.g., <code>if x=0</code> ... <code>else if x=1</code> ... <code>else if x=2</code>...), an alternative is the [[switch statement]], also called case-statement or select-statement. Conversely, in languages that do not have a switch statement, these can be produced by a sequence of <code>else if</code> statements. ===If–then–else expressions=== {{See also|Ternary conditional operator}} Many languages support ''conditional expressions'', which are similar to if statements, but return a value as a result. Thus, they are true expressions (which evaluate to a value), not statements (which may not be permitted in the context of a value). The concept of conditional expressions was first developed by [[John McCarthy (computer scientist)|John McCarthy]] during his research into symbolic processing and LISP in the late 1950s. ====Algol family==== [[ALGOL 60]] and some other members of the [[ALGOL]] family allow <code>if–then–else</code> as an expression. The idea of including conditional expressions was suggested by John McCarthy, though the ALGOL committee decided to use English words rather than McCarthy's mathematical syntax: <pre> myvariable := if x > 20 then 1 else 2 </pre> <!-- Don't place a semicolon at the end of the above ALGOL statement. It is NOT C. --> ====Lisp dialects==== Conditional expressions have always been a fundamental part of [[Lisp (programming language)|Lisp]]{{nbsp}}. In pure LISP, the <code>COND</code> function is used. In dialects such as [[Scheme (programming language)|Scheme]], [[Racket (programming language)|Racket]] and [[Common Lisp]]{{nbsp}}: <syntaxhighlight lang="scheme"> ;; Scheme (define (myvariable x) (if (> x 12) 1 2)) ; Assigns 'myvariable' to 1 or 2, depending on the value of 'x' </syntaxhighlight> <syntaxhighlight lang="lisp"> ;; Common Lisp (let ((x 10)) (setq myvariable (if (> x 12) 2 4))) ; Assigns 'myvariable' to 2 </syntaxhighlight> {{See also|McCarthy Formalism}} ====Haskell==== {{Main|Haskell (programming language)}} In [[Haskell (programming language)|Haskell]] 98, there is only an ''if expression'', no ''if statement'', and the <code>else</code> part is compulsory, as every expression must have some value.<ref name="haskell98report">''[http://www.haskell.org/onlinereport/ Haskell 98 Language and Libraries: The Revised Report]''</ref> Logic that would be expressed with conditionals in other languages is usually expressed with [[pattern matching]] in recursive functions. Because Haskell is [[lazy evaluation|lazy]], it is possible to write control structures, such as ''if'', as ordinary expressions; the lazy evaluation means that an ''if function'' can evaluate only the condition and proper branch (where a strict language would evaluate all three). It can be written like this:<ref name="haskell-ifthenelse-proposal">"[http://haskell.org/haskellwiki/If-then-else If-then-else Proposal on HaskellWiki]"</ref> <syntaxhighlight lang="haskell"> if' :: Bool -> a -> a -> a if' True x _ = x if' False _ y = y </syntaxhighlight> ====C-like languages==== [[C (programming language)|C]] and C-like languages have a special [[ternary operator]] ([[?:]]) for conditional expressions with a function that may be described by a template like this: <code>condition ? evaluated-when-true : evaluated-when-false</code> This means that it can be inlined into expressions, unlike if-statements, in C-like languages: <syntaxhighlight lang="c"> my_variable = x > 10 ? "foo" : "bar"; // In C-like languages </syntaxhighlight> which can be compared to the Algol-family if–then–else ''expressions'' (in contrast to a ''statement'') (and similar in Ruby and Scala, among others). To accomplish the same using an if-statement, this would take more than one line of code (under typical layout conventions), and require mentioning "my_variable" twice: <syntaxhighlight lang="c"> if (x > 10) my_variable = "foo"; else my_variable = "bar"; </syntaxhighlight> Some argue that the explicit if/then statement is easier to read and that it may compile to more efficient code than the ternary operator,<ref>{{cite web|url=http://embeddedgurus.com/stack-overflow/2009/02/efficient-c-tips-6-dont-use-the-ternary-operator/ |title=Efficient C Tips #6 – Don't use the ternary operator « Stack Overflow |publisher=Embeddedgurus.com |date=2009-02-18 |access-date=2012-09-07}}</ref> while others argue that concise expressions are easier to read than statements spread over several lines containing repetition. ====[[Small Basic]]==== <syntaxhighlight lang="vbnet"> x = TextWindow.ReadNumber() If (x > 10) Then TextWindow.WriteLine("My variable is named 'foo'.") Else TextWindow.WriteLine("My variable is named 'bar'.") EndIf </syntaxhighlight> First, when the user runs the program, a cursor appears waiting for the reader to type a number. If that number is greater than 10, the text "My variable is named 'foo'." is displayed on the screen. If the number is smaller than 10, then the message "My variable is named 'bar'." is printed on the screen. ====Visual Basic==== {{Main|Visual Basic}} In [[Visual Basic]] and some other languages, a function called <code>[[IIf]]</code> is provided, which can be used as a conditional expression. However, it does not behave like a true conditional expression, because both the true and false branches are always evaluated; it is just that the result of one of them is thrown away, while the result of the other is returned by the IIf function. ====Tcl==== {{Main|Tcl}} In [[Tcl]] <code>if</code> is not a keyword but a function (in Tcl known as command or <code>proc</code>). For example <syntaxhighlight lang="tcl"> if {$x > 10} { puts "Foo!" } </syntaxhighlight> invokes a function named <code>if</code> passing 2 arguments: The first one being the condition and the second one being the true branch. Both arguments are passed as strings (in Tcl everything within curly brackets is a string). In the above example the condition is not evaluated before calling the function. Instead, the implementation of the <code>if</code> function receives the condition as a string value and is responsible to evaluate this string as an expression in the callers scope.<ref>{{cite web|title=New Control Structures|url=https://wiki.tcl-lang.org/page/New+Control+Structures|publisher=[[Tcler's wiki]]|access-date=August 21, 2020}}</ref> Such a behavior is possible by using <code>uplevel</code> and <code>expr</code> commands: :Uplevel makes it possible to implement new control constructs as Tcl procedures (for example, uplevel could be used to implement the while construct as a Tcl procedure).<ref>{{cite web|title=uplevel manual page|url=https://www.tcl.tk/man/tcl8.6/TclCmd/uplevel.htm|publisher=[[www.tcl.tk]]|access-date=August 21, 2020}}</ref> Because <code>if</code> is actually a function it also returns a value: :The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN.<ref>{{cite web|title=if manual page|url=http://www.tcl.tk/man/tcl8.6/TclCmd/if.htm|publisher=[[www.tcl.tk]]|access-date=August 21, 2020}}</ref> ====Rust==== {{Main|Rust (programming language)}} In [[Rust (programming language)|Rust]], <code>if</code> is always an expression. It evaluates to the value of whichever branch is executed, or to the unit type <code>()</code> if no branch is executed. If a branch does not provide a return value, it evaluates to <code>()</code> by default. To ensure the <code>if</code> expression's type is known at compile time, each branch must evaluate to a value of the same type. For this reason, an <code>else</code> branch is effectively compulsory unless the other branches evaluate to <code>()</code>, because an <code>if</code> without an <code>else</code> can always evaluate to <code>()</code> by default.<ref>{{cite web|title=If and if let expressions|url=https://doc.rust-lang.org/reference/expressions/if-expr.html|access-date=November 1, 2020}}</ref> <syntaxhighlight lang="rust"> // Assign my_variable some value, depending on the value of x let my_variable = if x > 20 { 1 } else { 2 }; // This variant will not compile because 1 and () have different types let my_variable = if x > 20 { 1 }; // Values can be omitted when not needed if x > 20 { println!("x is greater than 20"); } </syntaxhighlight> ===Guarded conditionals=== The [[Guarded Command Language]] (GCL) of [[Edsger W. Dijkstra|Edsger Dijkstra]] supports conditional execution as a list of commands consisting of a Boolean-valued [[Guard (computer science)|guard]] (corresponding to a ''condition'') and its corresponding statement. In GCL, exactly one of the statements whose guards is true is evaluated, but which one is arbitrary. In this code '''if''' G0 → S0 □ G1 → S1 ... □ Gn → Sn '''fi''' the G<sub>''i''</sub>'s are the guards and the S<sub>''i''</sub>'s are the statements. If none of the guards is true, the program's behavior is undefined. GCL is intended primarily for reasoning about programs, but similar notations have been implemented in [[Concurrent Pascal]] and [[Occam (programming language)|occam]]. === Arithmetic if === Up to [[Fortran 77]], the language Fortran has had an arithmetic ''if'' statement which jumps to one of three labels depending on whether its argument ''e'' is {{nowrap|''e'' < 0,}} {{nowrap|1=''e'' = 0,}} {{nowrap|''e'' > 0.}} This was the earliest conditional statement in Fortran.<ref name="fortran77">{{cite web |url = http://www.fortran.com/fortran/F77_std/rjcnf0001-sh-11.html#sh-11.4 |title = American National Standard Programming Language FORTRAN |access-date = 2007-09-09 |date = 1978-04-03 |archive-url = https://web.archive.org/web/20071011055359/http://www.fortran.com/fortran/F77_std/rjcnf0001-sh-11.html#sh-11.4 |archive-date = 2007-10-11 |url-status = dead }}</ref> ==== Syntax ==== <syntaxhighlight lang="fortran"> IF (e) label1, label2, label3 </syntaxhighlight> Where e is a numeric expression of type <code>integer</code>, <code>real</code>, or <code>double precision</code>. ==== Semantics ==== This is equivalent to this sequence, where ''e'' is evaluated only once. <syntaxhighlight lang="fortran"> IF (e .LT. 0) GOTO label1 IF (e .EQ. 0) GOTO label2 IF (e .GT. 0) GOTO label3 </syntaxhighlight> ==== Stylistics ==== Arithmetic ''if'' is an unstructured control statement, and is not used in [[structured programming]]. In practice it has been observed that most arithmetic <code>IF</code> statements reference the following statement with one or two of the labels. This was the only conditional control statement in the original implementation of Fortran on the [[IBM 704]] computer. On that computer the test-and-branch op-code had three addresses for those three states. Other computers would have "flag" registers such as positive, zero, negative, even, overflow, carry, associated with the last arithmetic operations and would use instructions such as 'Branch if accumulator negative' then 'Branch if accumulator zero' or similar. Note that the expression is evaluated ''once only'', and in cases such as integer arithmetic where overflow may occur, the overflow or carry flags would be considered also. === Object-oriented implementation in Smalltalk === In contrast to other languages, in [[Smalltalk]] the conditional statement is not a [[language construct]] but defined in the class <code>Boolean</code> as an abstract method that takes two parameters, both [[Closure (computer science)|closure]]s. <code>Boolean</code> has two subclasses, <code>True</code> and <code>False</code>, which both define the method, <code>True</code> executing the first closure only, <code>False</code> executing the second closure only.<ref name="Smalltalk conditionals">{{cite web |url = http://wiki.cs.uiuc.edu/VisualWorks/Conditional+Processing |title = VisualWorks: Conditional Processing |date = 2006-12-16 |access-date = 2007-09-09 |archive-url = https://web.archive.org/web/20071022201949/http://wiki.cs.uiuc.edu/VisualWorks/Conditional+Processing |archive-date = 2007-10-22 |url-status = dead }}</ref> <syntaxhighlight lang="smalltalk"> var = condition ifTrue: [ 'foo' ] ifFalse: [ 'bar' ] </syntaxhighlight> === JavaScript === [[JavaScript]] uses if-else statements similar to those in [[C syntax|C]] languages. A Boolean value is accepted within parentheses between the reserved ''if'' keyword and a left curly bracket. <syntaxhighlight lang="javascript"> if (Math.random() < 0.5) { console.log("You got Heads!"); } else { console.log("You got Tails!"); } </syntaxhighlight> The above example takes the conditional of <code>Math.random() < 0.5</code> which outputs <code>true</code> if a random float value between 0 and 1 is greater than 0.5. The statement uses it to randomly choose between outputting <code>You got Heads!</code> or <code>You got Tails!</code> to the console. Else and else-if statements can also be chained after the curly bracket of the statement preceding it as many times as necessary, as shown below: <syntaxhighlight lang="javascript"> var x = Math.random(); if (x < 1/3) { console.log("One person won!"); } else if (x < 2/3) { console.log("Two people won!"); } else { console.log("It's a three-way tie!"); } </syntaxhighlight> === Lambda calculus === In [[Lambda calculus]], the concept of an if-then-else conditional can be expressed using the following expressions: true = λx. λy. x false = λx. λy. y ifThenElse = (λc. λx. λy. (c x y)) # true takes up to two arguments and once both are provided (see [[currying]]), it returns the first argument given. # false takes up to two arguments and once both are provided(see [[currying]]), it returns the second argument given. # ifThenElse takes up to three arguments and once all are provided, it passes both second and third argument to the first argument(which is a function that given two arguments, and produces a result). We expect ifThenElse to only take true or false as an argument, both of which project the given two arguments to their preferred single argument, which is then returned. '''note''': if ifThenElse is passed two functions as the left and right conditionals; it is '''necessary''' to also pass an empty tuple () to the result of ifThenElse in order to actually call the chosen function, otherwise ifThenElse will just return the function object without getting called. In a system where numbers can be used without definition (like Lisp, Traditional paper math, so on), the above can be expressed as a single closure below: <syntaxhighlight lang="lisp"> ((λtrue. λfalse. λifThenElse. (ifThenElse true 2 3) )(λx. λy. x)(λx. λy. y)(λc. λl. λr. c l r)) </syntaxhighlight> Here, true, false, and ifThenElse are bound to their respective definitions which are passed to their scope at the end of their block. A working JavaScript analogy(using only functions of single variable for rigor) to this is as follows: <syntaxhighlight lang="JavaScript"> var computationResult = ((_true => _false => _ifThenElse => _ifThenElse(_true)(2)(3) )(x => y => x)(x => y => y)(c => x => y => c(x)(y))); </syntaxhighlight> The code above with multivariable functions looks like this: <syntaxhighlight lang="JavaScript"> var computationResult = ((_true, _false, _ifThenElse) => _ifThenElse(_true, 2, 3) )((x, y) => x, (x, y) => y, (c, x, y) => c(x, y)); </syntaxhighlight> Another version of the earlier example without a system where numbers are assumed is below. The first example shows the first branch being taken, while second example shows the second branch being taken. <syntaxhighlight lang="lisp"> ((λtrue. λfalse. λifThenElse. (ifThenElse true (λFirstBranch. FirstBranch) (λSecondBranch. SecondBranch)) )(λx. λy. x)(λx. λy. y)(λc. λl. λr. c l r)) ((λtrue. λfalse. λifThenElse. (ifThenElse false (λFirstBranch. FirstBranch) (λSecondBranch. SecondBranch)) )(λx. λy. x)(λx. λy. y)(λc. λl. λr. c l r)) </syntaxhighlight> Smalltalk uses a similar idea for its true and false representations, with True and False being singleton objects that respond to messages ifTrue/ifFalse differently. Haskell used to use this exact model for its Boolean type, but at the time of writing, most Haskell programs use syntactic sugar "if a then b else c" construct which unlike ifThenElse does not compose unless either wrapped in another function or re-implemented as shown in The Haskell section of this page.
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)