FACTOID # 168: There are 11 countries where the average woman has more than six children. Ten of them are in Africa.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Conditional (programming)

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. Then: The Earlier Years is a double album by the band They Might Be Giants. ... Keep It Like a Secret album cover Keep It Like a Secret was the fifth full-length (and second major label) album released by indie rock band Built to Spill. ... Computer scaence, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems. ... A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ... In computer science, the Boolean datatype, sometimes called the logical datatype, is a primitive datatype having two values: one and zero (which are equivalent to true and false). ... Branch predication, not to be confused with branch prediction, is a strategy in computer architecture design for mitigating the costs usually associated with conditional branches, particularly branches to short sections of code. ... In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements or instructions of an imperative program are performed or executed. ...


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. In computer science, imperative programming, as opposed to declarative programming, is a programming paradigm that describes computation in terms of a program state and statements that change the program state. ... Functional programming is a programming paradigm that conceives computation as the evaluation of mathematical functions and avoids state and mutable data. ...


Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime. In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. ... In computer science, run time (with a space, though often its spelled without one) describes the operation of a computer program, the duration of its execution, from beginning to termination (compare compile time). ...

Contents

If-Then(-Else)

The if-then construct (sometimes called if-then-else) is common across many programming languages. Although the syntax varies quite a bit from language to language, the basic structure (in pseudocode form) looks like this: Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. ...

 If (condition) Then (statements) Else (statements) End If 

When an interpreter finds an If, it expects a boolean condition - for example, x > 0, which means "the variable x contains a number that is greater than zero" - and evaluates that condition. If the condition is true, the statement block following the Then is executed. Otherwise, the execution continues in the following block - either in the Else block (which is usually optional), or if there is no "Else" block, then after the End If. An interpreter is a computer program that executes other programs. ... In computer science, the Boolean datatype, sometimes called the logical datatype, is a primitive datatype having two values: one and zero (which are equivalent to true and false). ... In computer programming, a statement block (or code block) is a section of code which is grouped together, much like a paragraph; such blocks consist of one, or more, statements. ...


After either the block after the Then or the block after the Else has been executed, control returns to the point after the End If. In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements or instructions of an imperative program are performed or executed. ...


In early programming languages - and in particular, in some dialects of BASIC in the 1980s - an if-then statement could only contain GOTO statements. This led to a hard-to-read style of programming known as spaghetti programming. As a result, structured programming, which allowed (virtually) arbitrary statements to be put in statement blocks inside an if statement, gained in popularity, until it became the norm. BASIC is a family of high-level programming languages. ... This article cites very few or no references or sources. ... Goto may mean: GOTO (also known as Goto or Go to) – a branching construct in programming languages, infamous for its role in unstructured dialects of BASIC Goto, Nagasaki – a Japanese city G0-T0 (note: the characters following the G and T, respectively, are zeros), alias his coverup identity of Goto... A plate of spaghetti looks twisted and tangled, which is where the name for spaghetti code comes from. ... Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. ...


Else If parts

By using Else If, 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. The statements of the final Else will be executed if none of the conditions are true. This example is written in the Ada programming language: Ada is a structured, statically typed imperative computer programming language designed by a team led by Jean Ichbiah of CII Honeywell Bull under contract to the United States Department of Defense during 1977–1983. ...

 if condition then statements; elseif condition then more statements; elseif condition then more statements; ... else condition then other statements; end if; 

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. Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its expressiveness but make it sweeter for humans to use. ...


In some other languages, such as C, else if literally just means else followed by if - so no syntactic sugar is needed or provided. C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...


If expressions

Many 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

Main article: ?: [sic]

In C and C-like languages conditional expressions take the form of a ternary operator called the conditional expression operator, ?:, which follows this template: C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... In mathematics, a ternary operation is any operation of arity three, that is, that takes three arguments. ...

 (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 function

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. Visual Basic (VB) is an event driven programming language and associated development environment from Microsoft for its COM programming model. ... In computing, IIf is a function in several editions of the Visual Basic programming language that returns one of its two parameters based on the evaluation of an expression. ...


In Haskell

In Haskell 98, there is only an if expression, no if statement, and the else part is compulsory.[1] Haskell is a standardized purely functional programming language with non-strict semantics, named after the logician Haskell Curry. ...


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: Fortran (also FORTRAN) is a statically typed, compiled, programming language originally developed in the 1950s and still heavily used for scientific computing and numerical computation half a century later. ... A trichotomy is a splitting into three parts, and, apart from its normal literal meaning, can refer to: trichotomy (mathematics), in the mathematical field of order theory trichotomy (philosophy), for the idea that man has a threefold nature In taxonomy, a trichotomy is speciation of three groups from a common...

 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 GOTO statements, it is considered to be an unstructured control statement, and should not be used if more structured statements can be used. In practice it has been observed that most arithmetic IF statements referenced 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 it could be implemented quite efficiently using instructions such as 'Branch if accumulator negative'. The IBM 704, the first mass-produced computer with floating point arithmetic hardware, was introduced by IBM in April, 1956. ...


Alternative implementation

In contrast to other languages, in Smalltalk the conditional statement is not a language construct but defined as abstract method in the class Boolean as a method which 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. For other uses, see Small Talk (disambiguation). ... For closure in computer science, see closure (computer science). ...

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

Case and switch statements

Main article: Switch statement

Switch 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. In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e. ... In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e. ... Wikibooks has a book on the topic of Pascal Pascal is an imperative computer programming language, developed in 1970 by Niklaus Wirth as a language particularly suitable for structured programming. ...

 case someChar of switch (someChar) { 'a': actionOnA; case 'a': actionOnA; 'x': actionOnX; break; 'y','z':actionOnYandZ; case 'x': actionOnX; end; break; case 'y': case 'z': actionOnYandZ; break; default: actionOnNoMatch; } 

Pattern matching

Main article: Pattern matching

Pattern 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: Pattern matching is the act of checking for the presence of the constituents of a given pattern. ... Pattern matching is the act of checking for the presence of the constituents of a given pattern. ... ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage as it was conceived to develop proof tactics in the LCF theorem prover (the language of... Objective Caml (OCaml) is an advanced programming language which is a member of the ML family. ...

 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: Haskell is a standardized purely functional programming language with non-strict semantics, named after the logician Haskell Curry. ...

 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 predication

Main article: Branch predication

In 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. Branch predication, not to be confused with branch prediction, is a strategy in computer architecture design for mitigating the costs usually associated with conditional branches, particularly branches to short sections of code. ... See the terminology section, below, regarding inconsistent use of the terms assembly and assembler. ... Branch predication, not to be confused with branch prediction, is a strategy in computer architecture design for mitigating the costs usually associated with conditional branches, particularly branches to short sections of code. ... Die of an Intel 80486DX2 microprocessor (actual size: 12×6. ...


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.

Programming language Structured if Constant choice Arithmetic if Pattern matching[1]
then else else-if
Ada Yes Yes Yes Yes No  ?
C and C++ Yes Yes not needed [2] fall-through No No
Fortran Yes Yes Yes Yes Yes  ?
Perl Yes Yes Yes No No  ?
PHP Yes Yes Yes fall-through No  ?
Haskell Yes Yes, required not needed [2] not needed [3] No Yes
  1. a  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. a  b  The often-encountered else if in the C family of languages, and in 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.
  3. a  In Haskell, a separate constant choice construct is not needed, because the same task can be done with pattern matching.

A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ... Ada is a structured, statically typed imperative computer programming language designed by a team led by Jean Ichbiah of CII Honeywell Bull during 1977–1983. ... C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose, high-level programming language with low-level facilities. ... Fortran (previously FORTRAN[1]) is a general-purpose[2], procedural,[3] imperative programming language that is especially suited to numeric computation and scientific computing. ... Wikibooks has a book on the topic of Perl Programming Perl is a dynamic programming language created by Larry Wall and first released in 1987. ... PHP (PHP:Hypertext Preprocessor) is a reflective programming language originally designed for producing dynamic web pages. ... Haskell is a standardized purely functional programming language with non-strict semantics, named after the logician Haskell Curry. ... In computing, a regular expression is a string that is used to describe or match a set of strings, according to certain syntax rules. ...

See also

Look up then, else in Wiktionary, the free dictionary.

Wikipedia does not have an article with this exact name. ... Wiktionary (a portmanteau of wiki and dictionary) is a multilingual, Web-based project to create a free content dictionary, available in over 150 languages. ... In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. ...

References

  1. ^ Haskell 98 Language and Libraries: The Revised Report


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m