Professional Documents
Culture Documents
Conditional (Computer Programming) - Wikipedia PDF
Conditional (Computer Programming) - Wikipedia PDF
(computer
programming)
If–then(–else)
The if–then construct (sometimes
called if–then–else ) is common
across many programming languages.
Although the syntax varies from language
to language, the basic structure (in
pseudocode form) looks like this:
can be parsed as
if a then (if
if b then s)
else s2
or
if a then (if
if b then s else
s2)
Else if …
if condition then
--statements
elseif condition then
-- more statements
elseif condition then
-- more statements;
...
else
-- other statements;
end if
if;
The statements of elseif , in Ada, is
simply syntactic sugar for else
followed by if . In Ada, the difference is
that only one end if is needed, if one
uses elseif instead of else
followed by if . PHP uses the
elseif keyword[1] both for its curly
brackets or colon syntaxes. Perl provides
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. Some implementations of
BASIC, such as Visual Basic[2], use
ElseIf too. 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.
If–then–else expressions …
myvariable := if x > 10
then 1 else 2
Lisp dialects …
;; Common Lisp
(let
let ((x 10))
(setq
setq myvariable (if
if (> x
12) 2 4))) ; Assigns
'myvariable' to 2
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.[4] Logic that would be
expressed with conditionals in other
languages is usually expressed with
pattern matching in recursive functions.
C-like languages …
condition ? evaluated-when-
true : evaluated-when-false
my_variable = x > 10 ?
"foo" : "bar"; // In C-
like languages
In Small Basic …
x = TextWindow.ReadNumber()
If (x > 10) Then
TextWindow.WriteLine("My
variable is named 'foo'.")
Else
TextWindow.WriteLine("My
variable is named 'bar'.")
EndIf
In Visual Basic …
var = condition
ifTrue: [ 'foo' ]
ifFalse: [ 'bar' ]
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!");
}
Lambda Calculus
In Lambda Calculus, the concept of an if-
then-else conditional can be expressed
using the expressions:
true = λx. λy. x
false = λx. λy. y
ifThenElse = (λc. λx. λy. (c
x y))
((λtrue. λfalse.
λifThenElse.
(ifThenElse true 2 3)
)(λx. λy. x)(λx. λy. y)(λc.
λl. λr. c l r))
var computationResult =
((_true => _false =>
_ifThenElse =>
_ifThenElse(_true)(2)
(3)
)(x => y => x)(x => y =>
y)(c => x => y => c(x)
(y)));
((λtrue. λfalse.
λifThenElse.
(ifThenElse false
(λFirstBranch. FirstBranch)
(λSecondBranch.
SecondBranch))
)(λx. λy. x)(λx. λy. y)(λc.
λl. λr. c l r))
case $someChar
switch (someChar) {
in
case 'a':
a)
case someChar of actionOnA; break
break;
actionOnA; ;;
'a': actionOnA; case 'x':
x)
'x': actionOnX; actionOnX; break
break;
actionOnX; ;;
case 'y':
[yz])
'y','z':actionOnYandZ; case 'z':
actionOnYandZ;
else actionOnYandZ;
;;
actionOnNoMatch; break;
break
*)
end;
end default:
default
actionOnNoMatch
actionOnNoMatch;
;;
}
esac
Pattern matching
Pattern matching may be seen as an
alternative to both if–then–else, and case
statements. It is available in many
programming languages with functional
programming features, such as Wolfram
Language, ML and many others. Here is a
simple example written in the OCaml
language:
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.[9]
Predication
An alternative to conditional branch
instructions is predication. Predication is
an architectural feature that enables
instructions to be conditionally executed
instead of modifying the control flow.
Yes, but
Haskell Yes Needed Unneeded[B] No Yes
unneeded[C]
ECMAScript
Yes Yes Unneeded[B] Fall-through[11] No No
(JavaScript)
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
Test (Unix)
Yoda conditions
Conditional move
References
1. PHP elseif syntax
2. Visual Basic ElseIf syntax
3. POSIX standard shell syntax
4. Haskell 98 Language and Libraries:
The Revised Report
5. "If-then-else Proposal on HaskellWiki "
6. "Efficient C Tips #6 – Don't use the
ternary operator « Stack Overflow" .
Embeddedgurus.com. 2009-02-18.
Retrieved 2012-09-07.
7. "American National Standard
Programming Language FORTRAN" .
1978-04-03. Archived from the
original on 2007-10-11. Retrieved
2007-09-09.
8. "VisualWorks: Conditional
Processing" . 2006-12-16. Archived
from the original on 2007-10-22.
Retrieved 2007-09-09.
9. "Pythonic way to implement
switch/case statements" . Archived
from the original on 2015-01-20.
Retrieved 2015-01-19.
10. Java.sun.com , Java Language
Specification, 3rd Edition.
11. Ecma-international.org Archived
2015-04-12 at the Wayback Machine
ECMAScript Language Specification,
5th Edition.
External links
Look up then or else in Wiktionary, the
free dictionary.
Retrieved from
"https://en.wikipedia.org/w/index.php?
title=Conditional_(computer_programming)&oldid=
935783705"