Conditional (computer programming)
In computer programming, a conditional statement directs program control flow based on the value of a condition; a Boolean expression. A conditional expression evaluates to a value without the side-effect of changing control flow.
Many programming languages (such as C) have distinct conditional statements and expressions. In pure functional programming, a conditional expression does not have side-effects, many functional programming languages with conditional expressions (such as Lisp) support side-effects.
Conditional statementTemplate:Anchor
If-then-else statementTemplate:Anchor
Template:Redirect
Although the syntax of an if-then-else statement varies by language, the general syntax is shown as pseudocode below. The part represented by the condition placeholder is an expression that evaluates to either true or false. If true, control passes to consequent and when complete to after Template:Code. If false, control passes to alternative and when complete to after Template:Code. As the else clause is optional, the else alternative part can be omitted. Typically, both consequent and alternative can be either a single statement or a block of statements.
<syntaxhighlight lang="lua"> if condition then
consequent
else
alternative
end if </syntaxhighlight>
The following example, also in pseudocode, replaces placeholders with example logic.
<syntaxhighlight lang="lua"> if stock = 0 then
message = 'order new stock'
else
message = 'there is stock'
end if </syntaxhighlight>
History and development
In early programming languages, especially dialects of BASIC, an if–then-else statement could only contain goto statements but this tended to result in hard-to-read spaghetti code. As a result, structured programming, which supports control flow via code blocks, gained in popularity, until it became the norm in most BASIC variants and all languages. Such mechanisms and principles were based on the ALGOL family of languages, including Pascal and Modula-2. While it is possible to use goto in a structured way, structured programming makes this easier. A structured if–then–else statement is one of the key elements of structured programming, and it is present in most popular languages such as C, Java, JavaScript and Visual Basic.
Dangling else
Template:Main The convention is that an Template:Code clause, like the Template:Code clause, responds to the nearest preceding Template:Code clause; however, the semantics in some early languages, such as ALGOL 60, of nested conditionals, were less than clear; that is to say, the syntax was inadequate to specify one-and-always-the-same predicate Template:Code clause. Thus, the parser might randomly pair the Template:Code with any one of the perhaps manifold Template:Code clauses in the intended nested hierarchy.
This is known as the dangling else problem. It is resolved in various ways, depending on the language (in some, by means of explicit block-ending syntax (such as Template:Code ) or a block enclosure, such as curly brackets ( {⋯} ).
ChainingTemplate:Anchor
Chaining conditionals is often provided in a language via an else-if construct. Only the statements following the first condition that is true are executed. Other statements are skipped. In placeholder pseudocode:
<syntaxhighlight lang="lua"> if condition1 then
block1
else if condition2 then
block2
else if condition3 then
block3 ...
else
block4
end if </syntaxhighlight>
In the following pseudocode, a shop offers as much as a 30% discount for an item. If the discount is 10%, then the first if statement is true and "Template:Samp" is printed. All other statements below that first if statement are skipped.
<syntaxhighlight lang="lua"> if discount < 11% then
print "You have to pay $30."
else if discount < 21% then
print "You have to pay $20."
else if discount < 31% then
print "You have to pay $10."
end if </syntaxhighlight>
Only one Template:Code is needed if one uses Template:Code instead of Template:Code followed by Template:Code.
In ALGOL 68, the 1968 “Draft Report” (circulated as a supplement to ALGOL Bulletin no. 26) still used the bold keyword Template:Mono in “contracted” conditionals.<ref>Template:Cite report</ref>
The spelling Template:Mono was then standardized in the “Revised Report on the Algorithmic Language ALGOL 68” (1973), which lists both the Template:Mono words Template:Nowrap and their “brief” symbols, where Template:Mono corresponds to |: in the compact form Template:Nowrap begin( ~ | ~ |: ~ | ~ | ~ ).Template:Nowrap end<ref>Template:Cite journal</ref>
In Ada, the Template:Mono keyword is syntactic sugar for the two words Template:Code. PHP also supports an Template:Code keyword<ref name="php_elseif">PHP elseif syntax</ref> both for its curly brackets or colon syntaxes. Perl and Ruby provide the keyword Template:Perl2 to avoid the large number of braces that would be required by multiple Template:Perl2 and Template:Perl2 statements. Python uses the special keyword Template:Python because structure is denoted by indentation rather than braces, so a repeated use of Template:Python and Template:Python would require increased indentation after every condition. Visual Basic, supports Template:Code.<ref name="vb_elseif">Visual Basic Template:Mono syntax</ref> Similarly, the earlier UNIX shells (later gathered up to the POSIX shell syntax<ref name="posixshell">POSIX standard shell syntax</ref>) use Template:Mono 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, Pascal, BCPL and C, this special syntax for the Template:Code construct is not present, nor is it present in the many syntactical derivatives of C, such as Java, ECMAScript, and so on. This works because in these languages, any single statement (in this case if cond...) can follow a conditional without being enclosed in a block.Template:Clarify
If all terms in the sequence of conditionals are testing the value of a single expression (e.g., Template:Nowrap ...), an alternative is the switch statement. In a language that does not have a switch statement, these can be encoded as a chained if-then-else.
Switch statementTemplate:Anchor
A switch statement supports multiway branching; often comparing the value of an expression with constant values and transferring control to the code of the first match. There is usually a provision for a default action if no match is found. An optimizing compiler may use a control table to implement the logic of a switch statement. In a dynamic language, 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.
Guarded conditional
The Guarded Command Language (GCL) of Edsger Dijkstra supports conditional execution as a list of commands consisting of a Boolean-valued 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 Gi's are the guards and the Si'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.
Arithmetic if
The earliest conditional statement in Fortran, up to Fortran 77, was the arithmetic if statement which jumped to one of three labels depending on whether a value (of type integer, real, or double precision) is <0, 0, or >0.<ref name="fortran77">Template:Cite web</ref>
In the following code, control passes to one of the labels based on the value of Template:Code.
<syntaxhighlight lang="fortran">
IF (e) label1, label2, label3
</syntaxhighlight>
This is equivalent to the following sequence.
<syntaxhighlight lang="fortran">
e_temp = e
IF (e_temp .LT. 0) GOTO label1
IF (e_temp .EQ. 0) GOTO label2
IF (e_temp .GT. 0) GOTO label3
</syntaxhighlight>
As it acts like goto, arithmetic if is unstructured; not structured programming. It was the only conditional 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.
The Arithmetic IF statement was listed as obsolescent starting with the Fortran 90 Standard. It was deleted from the Fortran 2018 Standard. Nonetheless most compilers continue to support it for compatibility with legacy codes.
In Smalltalk
In contrast to other languages, in Smalltalk the conditional statement is not a language construct but defined in the class Boolean as an abstract method that takes two parameters, both closures. Boolean has two subclasses, True and False, which both define the method, True executing the first closure only, False executing the second closure only.<ref name="Smalltalk conditionals">Template:Cite web</ref>
<syntaxhighlight lang="smalltalk"> var = condition
ifTrue: [ 'foo' ] ifFalse: [ 'bar' ]
</syntaxhighlight>
In JavaScript
JavaScript supports if-else statements similar to C syntax. The following example has conditional Template:Jscode which is true if the random float (value between 0 and 1) is greater than 0.5. The statement uses it to randomly choose between outputting Template:Samp or Template:Samp.
<syntaxhighlight lang="javascript"> if (Math.random() < 0.5) {
console.log("You got Heads!");
} else {
console.log("You got Tails!");
} </syntaxhighlight>
Conditionals can be chained 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))
- Template:Mono takes up to two arguments and once both are provided (see currying), it returns the first argument given.
- Template:Mono takes up to two arguments and once both are provided(see currying), it returns the second argument given.
- Template:Mono 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 Template:Mono 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 Template:Mono is passed two functions as the left and right conditionals; it is necessary to also pass an empty tuple Template:Code to the result of Template:Mono in order to actually call the chosen function, otherwise Template:Mono 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, Template:Mono and Template:Mono 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 Template:Smalltalk and Template:Smalltalk being singleton objects that respond to messages Template:Mono differently.
Haskell used to use this exact model for its Boolean type, but at the time of writing, most Haskell programs use syntactic sugar Template:Haskell construct which unlike Template:Mono does not compose unless either wrapped in another function or re-implemented as shown in The Haskell section of this page.
Conditional expressionTemplate:Anchor
Template:See also Many languages support a conditional expression, which unlike a statement evaluates to a value instead of controlling control flow. The concept of conditional expression was first developed by John McCarthy during his research into symbolic processing and LISP in the late 1950s.
Examples
Algol
ALGOL 60 and some other members of the ALGOL family allow if–then–else 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:
<syntaxhighlight lang="pascal">
myvariable := if x > 20 then 1 else 2
</syntaxhighlight>
Compound statements are all terminated (guarded) by distinctive closing brackets:
- IF choice clauses:
IF condition THEN statements [ ELSE statements ] FI "brief" form: ( condition | statements | statements )
IF condition1 THEN statements ELIF condition2 THEN statements [ ELSE statements ] FI "brief" form: ( condition1 | statements |: condition2 | statements | statements )
This scheme not only avoids the dangling else problem but also avoids having to use BEGIN and END in embedded statement sequences.
- CASE choice clauses:
CASE switch IN statements, statements,... [ OUT statements ] ESAC "brief" form: ( switch | statements,statements,... | statements )
CASE switch1 IN statements, statements,... OUSE switch2 IN statements, statements,... [ OUT statements ] ESAC "brief" form of CASE statement: ( switch1 | statements,statements,... |: switch2 | statements,statements,... | statements )
Choice clause example with Brief symbols:
PROC days in month = (INT year, month)INT:
(month|
31,
(year÷×4=0 ∧ year÷×100≠0 ∨ year÷×400=0 | 29 | 28 ),
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);
Lisp
Conditional expressions have always been a fundamental part of LispTemplate:Nbsp. In pure LISP, the COND function is used. In dialects such as Scheme, Racket and Common LispTemplate: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>
Haskell
In Haskell 98, there is only an if expression, no if statement, and the else part is compulsory, as every expression must have some value.<ref name="haskell98report">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, 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">"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 and related languages support a ternary operator that provides for conditional expressions like:
condition ? true-value : false-value
If condition is true, then the expression evaluates to true-value; otherwise to false-value. In the following code, r is assigned to "foo" if x > 10; otherwise to "bar".
<syntaxhighlight lang="c"> r = x > 10 ? "foo" : "bar"; </syntaxhighlight>
To accomplish the same using an if-statement, this would take more than one statement, and require mentioning Template:Code twice:
<syntaxhighlight lang="c"> if (x > 10)
r = "foo";
else
r = "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>Template:Cite web</ref> while others argue that concise expressions are easier to read and better since they have less repeated clauses.
Visual Basic
Template:Main
In Visual Basic and some other languages, a function called IIf 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
Template:Main
In Tcl if is not a keyword but a function (in Tcl known as command or proc). For example
<syntaxhighlight lang="tcl">
if {$x > 10} {
puts "Foo!"
}
</syntaxhighlight>
invokes a function named if 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 if function receives the condition as a string value and is responsible to evaluate this string as an expression in the callers scope.<ref>Template:Cite web</ref>
Such a behavior is possible by using uplevel and expr 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>Template:Cite web</ref>
Because if 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>Template:Cite web</ref>
Rust
In Rust, if is always an expression. It evaluates to the value of whichever branch is executed, or to the unit type () if no branch is executed. If a branch does not provide a return value, it evaluates to () by default. To ensure the if expression's type is known at compile time, each branch must evaluate to a value of the same type. For this reason, an else branch is effectively compulsory unless the other branches evaluate to (), because an if without an else can always evaluate to () by default.<ref>Template:Cite web</ref>
The following assigns Template:Code to 1 or 2 depending on the value of x.
<syntaxhighlight lang="rust"> let r = if x > 20 {
1
} else {
2
}; </syntaxhighlight>
Values can be omitted when not needed.
<syntaxhighlight lang="rust"> if x > 20 {
println!("x is greater than 20");
} </syntaxhighlight>
Pattern matching
Pattern matching is an alternative to conditional statements (such as if–then–else and switch). It is available in many languages with functional programming features, such as Wolfram Language, ML and many others. Here is a simple example written in the OCaml language:
<syntaxhighlight lang="ocaml"> match fruit with | "apple" -> cook pie | "coconut" -> cook dango_mochi | "banana" -> mix;; </syntaxhighlight>
The power of pattern matching is the ability to concisely match not only actions but also values to patterns of data. Here is an example written in Haskell which illustrates both of these features:
<syntaxhighlight lang="haskell"> map _ [] = [] map f (h : t) = f h : map f t </syntaxhighlight>
This code defines a function map, which applies the first argument (a function) to each of the elements of the second argument (a list), and returns the resulting list. The two lines are the two definitions of the function for the two kinds of arguments possible in this case – one where the list is empty (just return an empty list) and the other case where the list is not empty.
Pattern matching is not strictly speaking always a choice construct, because it is possible in Haskell to write only one alternative, which is guaranteed to always be matched – in this situation, it is not being used as a choice construct, but simply as a way to bind names to values. However, it is frequently used as a choice construct in the languages in which it is available.
Hash-based conditionals
In programming languages that have associative arrays or comparable data structures, such as Python, Perl, PHP or Objective-C, it is idiomatic to use them to implement conditional assignment.<ref>Template:Cite web</ref>
<syntaxhighlight lang="python"> pet = input("Enter the type of pet you want to name: ") known_pets = {
"Dog": "Fido", "Cat": "Meowsles", "Bird": "Tweety",
} my_name = known_pets[pet] </syntaxhighlight>
In languages that have anonymous functions or that allow a programmer to assign a named function to a variable reference, conditional flow can be implemented by using a hash as a dispatch table.
Branch predication
An alternative to conditional branch instructions is branch predication. Predication is an architectural feature that enables instructions to be conditionally executed instead of modifying the control flow.
Choice system cross reference
This table refers to the most recent language specification of each language. For languages that do not have a specification, the latest officially released implementation is referred to.
See also
- Branch (computer science)
- Conditional compilation
- Dynamic dispatch for another way to make execution choices
- McCarthy Formalism for history and historical references
- Named condition
- Relational operator
- Test (Unix)
- Yoda conditions
- Conditional move