|
This article is about computer programming; for conditional statements in general, see Logical conditional.
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified condition evaluates to true or false (see boolean datatype). Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition. In imperative programming languages, the term "conditional statement" is usually used, whereas in functional programming, the terms "conditional expression" or "conditional construct" are preferred, because these terms all have distinct meanings. Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime.
If-Then(-Else)The If (condition) Then (statements) Else (statements) End If When an interpreter finds an After either the block after the In early programming languages - and in particular, in some dialects of BASIC in the 1980s - an Else If partsBy using if condition then -- statements; elseif condition then -- more statements; elseif condition then -- more statements; ... else condition then -- other statements; end if;
In some other languages, such as C, If expressionsMany languages support if 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 just perform an action). As a ternary operator
In C and C-like languages conditional expressions take the form of a ternary operator called the conditional expression operator, ?:, which follows this template: (condition)?(evaluate if condition was true):(evaluate if condition was false) This means that conditions can be inlined into expressions, unlike with if statements, as shown here using C syntax: //Invalid my_variable = if(x > 10) { "foo" } else { "bar" }; //Valid my_variable = (x > 10)?"foo":"bar"; To accomplish the same as the second (correct) line above, using a standard if/else statement, this would take more than one line of code (under standard layout conventions): if (x > 10) { my_variable = 'foo'; } else { my_variable = 'bar'; } As a functionIn Visual Basic and some other languages, a function called In HaskellIn Haskell 98, there is only an if expression, no if statement, and the Arithmetic IF
Fortran 77 has an "arithmetic if" statement which is halfway between a computed IF and a case statement, based on the trichotomy x < 0, x = 0, x > 0[2]: IF (e) label1, label2, label3 Where e is any numeric expression (not necessarily an integer); this is equivalent to IF (e < 0) GOTO label1 IF (e = 0) GOTO label2 IF (e > 0) GOTO label3 Because this arithmetic IF is equivalent to multiple This was the only conditional control statement in the original implementation of Fortran on the IBM 704 computer. On that computer it could be implemented quite efficiently using instructions such as 'Branch if accumulator negative'. Alternative implementationIn contrast to other languages, in Smalltalk the conditional statement is not a language construct but defined in the class var := condition ifTrue: [ 'foo' ] ifFalse: [ 'bar' ] Case and switch statementsSwitch statements (in some languages, case statements) compare a given value with specified constants and take action according to the first constant to match. The example on the left is written in Pascal, and the example on the right is written in C.
Pattern matchingPattern matching is a more sophisticated alternative to both if-then-elseif, and the simple case or switch statements mentioned above. It is available in some programming languages such as the ML language family. Here is a simple example written in the O'Caml language: match fruit with | "apple" -> cook pie | "coconut" -> cook dango_mochi | "banana" -> mix;; The true power of pattern matching, however, comes from the ability to concisely (a) match on data structure patterns, and (b) bind variables at the same time. 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 not two separate definitions of the function, but rather definitions of what to do in two cases - one case 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 a choice construct, but simply as a way to bind variables. However, it is frequently used as a choice construct in the languages in which it is available. Branch predicationIn assembly language, branch predication is a feature of certain CPU instruction sets which permits conditional execution of instructions, without having to perform costly conditional jumps. Choice system cross referenceThis 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
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net