Jump to content

Conditional (computer programming)

From Wikipedia, the free encyclopedia
If-then-else flow diagram
A nested if–then–else flow diagram

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 statement

[edit]

If-then-else statement

[edit]

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 end if. If false, control passes to alternative and when complete to after end if. 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.

if condition then
   consequent
else
   alternative
end if

The following example, also in pseudocode, replaces placeholders with example logic.

if stock = 0 then
    message = order new stock
else
    message = there is stock
end if

History and development

[edit]

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

[edit]

An else clause targets a if–then clause that precedes it, but for nested conditionals, some languages such as ALGOL 60 resulted in less than clear semantics of which if clause the else targeted. Without clear boundaries for which statement is which, an else could target any preceding if clause in the nest, as parsed. For example:

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 else clause is associated with the first or second if. Known as the dangling else problem, this is resolved in various ways, depending on the language (commonly via a block end syntax (such as end if) or block enclosure such as curly brackets ({ }).

Chaining

[edit]

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:

 if condition1 then 
    block1
 elseif condition2 then 
    block2
 elseif condition3 then 
    block3;
...
 else
    block4
 end if

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 "you have to pay $30" is printed. All other statements below that first if statement are skipped.

 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 Ada, the elseif keyword is syntactic sugar for the two words else if. Only one end if is needed. If one uses elseif instead of else followed by if. PHP also supports an elseif keyword[1] both for its curly brackets or colon syntaxes. Perl and Ruby provide the keyword elsif to avoid the large number of braces that would be required by multiple if and else statements. Python uses the special keyword elif because structure is denoted by indentation rather than braces, so a repeated use of else and if would require increased indentation after every condition. Visual Basic, supports ElseIf.[2] Similarly, the earlier UNIX shells (later gathered up to the POSIX shell syntax[3]) 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, Pascal, BCPL and C, this special syntax for the else if 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.[clarification needed]

If all terms in the sequence of conditionals are testing the value of a single expression (e.g., if x=0 ... else if x=1 ... else if x=2...), 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 statement

[edit]

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

[edit]

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

[edit]

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.[4]

In the following code, control passes to one of the labels based on the value of e.

      IF (e) label1, label2, label3

This is equivalent to the following sequence.

      e_temp = e
      IF (e_temp .LT. 0) GOTO label1
      IF (e_temp .EQ. 0) GOTO label2
      IF (e_temp .GT. 0) GOTO label3

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.

In Smalltalk

[edit]

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.[5]

var = condition 
    ifTrue: [ 'foo' ]
    ifFalse: [ 'bar' ]

In JavaScript

[edit]

JavaScript supports if-else statements similar to C syntax. The following example has conditional Math.random() < 0.5 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 You got Heads! or You got Tails!.

if (Math.random() < 0.5) {
  console.log("You got Heads!");
} else {
  console.log("You got Tails!");
}

Conditionals can be chained as shown below:

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!");
}

Lambda calculus

[edit]

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))
  1. true takes up to two arguments and once both are provided (see currying), it returns the first argument given.
  2. false takes up to two arguments and once both are provided(see currying), it returns the second argument given.
  3. 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:

((λtrue. λfalse. λifThenElse.
    (ifThenElse true 2 3)
)(λx. λy. x)(λx. λy. y)(λc. λl. λr. c l r))

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:

var computationResult = ((_true => _false => _ifThenElse => 
    _ifThenElse(_true)(2)(3) 
)(x => y => x)(x => y => y)(c => x => y => c(x)(y)));

The code above with multivariable functions looks like this:

var computationResult = ((_true, _false, _ifThenElse) =>
    _ifThenElse(_true, 2, 3)
)((x, y) => x, (x, y) => y, (c, x, y) => c(x, y));

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.

((λ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))

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.

Conditional expression

[edit]

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

[edit]

Algol

[edit]

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:

myvariable := if x > 20 then 1 else 2

Lisp

[edit]

Conditional expressions have always been a fundamental part of Lisp . In pure LISP, the COND function is used. In dialects such as Scheme, Racket and Common Lisp :

;; Scheme
(define (myvariable x) (if (> x 12) 1 2))   ; Assigns 'myvariable' to 1 or 2, depending on the value of 'x'
;; Common Lisp
(let ((x 10))
  (setq myvariable (if (> x 12) 2 4)))  ; Assigns 'myvariable' to 2

Haskell

[edit]

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.[6] 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:[7]

if' :: Bool -> a -> a -> a
if' True x _ = x
if' False _ y = y

C-like languages

[edit]

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".

r = x > 10 ? "foo" : "bar";

To accomplish the same using an if-statement, this would take more than one statement, and require mentioning r twice:

if (x > 10)
    r = "foo";
else
    r = "bar";

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,[8] while others argue that concise expressions are easier to read and better since they have less repeated clauses.

Visual Basic

[edit]

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

[edit]

In Tcl if is not a keyword but a function (in Tcl known as command or proc). For example

if {$x > 10} {
    puts "Foo!"
}

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.[9]

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).[10]

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.[11]

Rust

[edit]

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.[12]

The following assigns r to 1 or 2 depending on the value of x.

let r = if x > 20 {
    1
} else {
    2
};

Values can be omitted when not needed.

if x > 20 {
    println!("x is greater than 20");
}

Pattern matching

[edit]

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:

match fruit with
| "apple" -> cook pie
| "coconut" -> cook dango_mochi
| "banana" -> mix;;

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:

map _ []      = []
map f (h : t) = f h : map f t

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

[edit]

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.[13]

pet = input("Enter the type of pet you want to name: ")
known_pets = {
    "Dog": "Fido",
    "Cat": "Meowsles",
    "Bird": "Tweety",
}
my_name = known_pets[pet]

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

[edit]

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

[edit]

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.

Programming language Structured if switch–select–case Conditional expressions Arithmetic if Pattern matching[A]
then else else–if
Ada Yes Yes Yes Yes Yes No No
ALGOL-60 Yes Yes Unneeded[C] No Yes No No
APL No Yes Yes Yes Yes No No
Bash shell Yes Yes Yes Yes Yes No Yes
C, C++ No Yes Unneeded[B][C] Fall-through Yes No No
C# No Yes Unneeded[B][C] Yes Yes No No
COBOL Unneeded Yes Unneeded[C] Yes No No No
Eiffel Yes Yes Yes Yes Yes No No
F# Yes Yes Yes Yes[D] Yes No Yes
Fortran Yes Yes Yes Yes Yes[G] Yes[I] No
Go No Yes Unneeded[C] Yes No No No
Haskell Yes Needed Unneeded[C] Yes[D] Yes No Yes
Java No Yes Unneeded[C] Fall-through[14] Yes No No
ECMAScript (JavaScript) No Yes Unneeded[C] Fall-through[15] Yes No No
Mathematica No Yes Yes Yes Yes No Yes
Oberon Yes Yes Yes Yes No No No
Perl No Yes Yes Yes Yes No No
PHP No Yes Yes Fall-through Yes No Yes
Pascal, Object Pascal (Delphi) Yes Yes Unneeded Yes No No No
Python No Yes Yes Yes Yes No Yes
QuickBASIC Yes Yes Yes Yes No No No
Ruby Yes Yes Yes Yes Yes No Yes[H]
Rust No Yes Yes Yes[D] Yes No Yes
Scala No Yes Unneeded[C] Fall-through[citation needed] Yes No Yes
SQL Yes[F] Yes Yes Yes[F] Yes No No
Swift No Yes Yes Yes Yes No Yes
Tcl No Yes Yes Yes Yes No Yes
Visual Basic, classic Yes Yes Yes Yes Yes No No
Visual Basic .NET Yes Yes Yes Yes Yes No No
Windows PowerShell No Yes Yes Fall-through Yes No No
  1. ^ This refers to pattern matching as a distinct conditional construct in the programming language – as opposed to mere string pattern matching support, such as regular expression support.
  2. 1 2 An #ELIF directive is used in the preprocessor sub-language that is used to modify the code before compilation; and to include other files.
  3. 1 2 3 4 5 6 The often-encountered else if in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent if then else statements combined with a particular source code layout. However, this also means that a distinct else–if construct is not really needed in these languages.
  4. 1 2 3 Case-expressions in Haskell and match-expressions in F# and Haskell allow both switch-case and pattern matching usage.
  5. ^ In a Ruby case construct, regular expression matching is among the conditional flow-control alternatives available. For an example, see this Stack Overflow question.
  6. 1 2 SQL has two similar constructs that fulfill both roles, both introduced in SQL-92. A "searched CASE" expression CASE WHEN cond1 THEN expr1 WHEN cond2 THEN expr2 [...] ELSE exprDflt END works like if ... else if ... else, whereas a "simple CASE" expression: CASE expr WHEN val1 THEN expr1 [...] ELSE exprDflt END works like a switch statement. For details and examples see Case (SQL).
  7. ^ Fortran 90 added the MERGE intrinsic. Fortran 2023 added the C-like ternary operator.
  8. ^ Pattern matching was added in Ruby 3.0.[16] Some pattern matching constructs are still experimental.
  9. ^ Arithmetic if was marked as obsolescent in Fortran 90. It was deleted as of the Fortran 2018 Standard.

See also

[edit]

References

[edit]
  1. ^ PHP elseif syntax
  2. ^ Visual Basic ElseIf syntax
  3. ^ POSIX standard shell syntax
  4. ^ "American National Standard Programming Language FORTRAN". 1978-04-03. Archived from the original on 2007-10-11. Retrieved 2007-09-09.
  5. ^ "VisualWorks: Conditional Processing". 2006-12-16. Archived from the original on 2007-10-22. Retrieved 2007-09-09.
  6. ^ Haskell 98 Language and Libraries: The Revised Report
  7. ^ "If-then-else Proposal on HaskellWiki"
  8. ^ "Efficient C Tips #6 – Don't use the ternary operator « Stack Overflow". Embeddedgurus.com. 2009-02-18. Retrieved 2012-09-07.
  9. ^ "New Control Structures". Tcler's wiki. Retrieved August 21, 2020.
  10. ^ "uplevel manual page". www.tcl.tk. Retrieved August 21, 2020.
  11. ^ "if manual page". www.tcl.tk. Retrieved August 21, 2020.
  12. ^ "If and if let expressions". Retrieved November 1, 2020.
  13. ^ "Pythonic way to implement switch/case statements". Archived from the original on 2015-01-20. Retrieved 2015-01-19.
  14. ^ Java.sun.com, Java Language Specification, 3rd Edition.
  15. ^ Ecma-international.org Archived 2015-04-12 at the Wayback Machine ECMAScript Language Specification, 5th Edition.
  16. ^ "Pattern Matching". Documentation for Ruby 3.0.
[edit]