Citizendia

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). Computer science (or computing science) is the study and the Science of the theoretical foundations of Information and Computation and their A programming language is an Artificial language that can be used to write programs which 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 one of two values Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition. Branch predication is a strategy in Computer architecture design for mitigating the costs usually associated with conditional branches particularly branches to In Computer science control flow (or alternatively flow of control refers to the order in which the individual statements, instructions or Function

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 is a Programming paradigm that describes computation in terms of statements that change a program state In Computer programming a statement can be thought of as the smallest standalone element of an imperative Programming language. In Computer science, functional programming is a Programming paradigm that treats Computation as the evaluation of mathematical functions and An expression in a Programming language is a combination of values Variables operators and functions that are interpreted (

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, runtime or run time describes the operation of a Computer program, the duration of its execution from beginning to termination

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:

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. Pseudocode is a compact and informal high-level description of a Computer programming Algorithm that uses the structural conventions of some Programming language In Computer science, an interpreter normally means a Computer program that executes, i In Computer science, the Boolean datatype, sometimes called the logical datatype, is a Primitive datatype having one of two values If the condition is true, the statement block following the Then is executed. In Computer programming, a statement block (or code block) is a section of code which is grouped together much like a Paragraph; such blocks 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.

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, instructions or Function

In early programming languages - and in particular, in some dialects of BASIC in the 1980s - an if-then statement could only contain GOTO statements. In Computer programming, BASIC (an Acronym for Beginner's All-purpose Symbolic Instruction Code) is a family of High-level programming languages The 1980s was the decade spanning from January 1 1980 to December 31 1989. GOTO is a statement found in many computer Programming languages It is a combination of the English words go and to This led to a hard-to-read style of programming known as spaghetti programming. Spaghetti code is a Pejorative term for Source code which has a complex and tangled Control structure, especially one using many GOTOs exceptions 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. Structured programming can be seen as a subset or subdiscipline of Procedural programming, one of the major Programming paradigms It is most famous for removing or

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:

if condition then    -- statements;elseif condition then    -- more statements;elseif condition then    -- more statements;. Ada is a structured, Statically typed, imperative, and object-oriented high-level computer Programming language . . else condition then    -- other statements;end if;

elseif, in Ada, is simply syntactic sugar for 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 functionality In Ada, the difference is that only one end if is needed, if one uses elseif instead of else followed by if.

In some other languages, such as C, else if literally just means else followed by if - so no syntactic sugar is needed or provided. tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured

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:

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

//Invalidmy_variable = if(x > 10) { "foo" } else { "bar" };//Validmy_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. ? is a Ternary operator that is part of the syntax for a basic conditional expression in several Programming languages including tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured In Mathematics, a ternary operation is an N-ary operation with n = 3 ? is a Ternary operator that is part of the syntax for a basic conditional expression in several Programming languages including Visual Basic ( VB) is the third-generation event-driven programming language and associated development environment (IDE from In Computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic Programming language 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.

In Haskell

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

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 label1IF (e = 0) GOTO label2IF (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. Fortran (previously FORTRAN) is a general-purpose, procedural, imperative Programming language that is especially suited to The arithmetic IF statement has been for several decades a three-way arithmetic conditional statement, starting from the very early version ( 1957) of Fortran 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. The IBM 704, the first mass-produced Computer with Floating point arithmetic hardware was introduced by IBM in April 1954. On that computer it could be implemented quite efficiently using instructions such as 'Branch if accumulator negative'.

Alternative implementation

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. Smalltalk is an object-oriented, dynamically typed, reflective programming language. Boolean has two subclasses, True and False, which both define the method, True executing the first closure only, False executing the second closure only. [3]

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. 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 The example on the left is written in Pascal, and the example on the right is written in C. Pascal is an influential imperative and procedural Programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured

Pascal (programming language):C (programming language):
case someChar of  'a': actionOnA;  'x': actionOnX;  'y','z':actionOnYandZ;end;
switch (someChar) {  case 'a': actionOnA; break;  case 'x': actionOnX; 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. Pascal is an influential imperative and procedural Programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured In Computer science, pattern matching is the act of checking for the presence of the constituents of a given Pattern. In Computer science, pattern matching is the act of checking for the presence of the constituents of a given Pattern. It is available in some programming languages such as the ML language family. 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 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. Objective Caml ( OCaml) is the main implementation of the Caml Programming language, created by Xavier Leroy, Jérôme Vouillon 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. Haskell is a standardized Purely functional Programming language with non-strict semantics, named after the Logician Haskell Curry 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 is a strategy in Computer architecture design for mitigating the costs usually associated with conditional branches particularly branches to See the terminology section below for information regarding inconsistent use of the terms assembly and assembler Branch predication is a strategy in Computer architecture design for mitigating the costs usually associated with conditional branches particularly branches to

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 languageStructured ifConstant choiceArithmetic ifPattern matching[1]
thenelseelse-if
AdaYesYesYesYesNoNo
C, C++ and C#YesYesnot needed [2]fall-throughNoNo
FortranYesYesYesYesYesNo
PerlYesYesYesNoNoNo
PHPYesYesYesfall-throughNoNo
HaskellYesYes, requirednot needed [2]not needed [3]NoYes
Visual Basic .NETYesYesYesYesNoNo
Windows PowerShellYesYesYesfall-throughNoYes
  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. A programming language is an Artificial language that can be used to write programs which control the behavior of a machine particularly a Computer. Ada is a structured, Statically typed, imperative, and object-oriented high-level computer Programming language tags please moot on the talk page first! --> In Computing, C is a general-purpose cross-platform block structured C++ (" C Plus Plus " ˌsiːˌplʌsˈplʌs is a general-purpose Programming language. C# (pronounced C Sharp is a Multi-paradigm Fortran (previously FORTRAN) is a general-purpose, procedural, imperative Programming language that is especially suited to NOTES FOR EDITORS "Perl" is not an acronym (read the "Name" section below PHP is a computer Scripting language. Originally designed for producing Dynamic web pages it has evolved to include a Command line interface capability Haskell is a standardized Purely functional Programming language with non-strict semantics, named after the Logician Haskell Curry Visual Basic.NET ( VBNET) is an object-oriented Computer language that can be viewed as an evolution of Microsoft's Visual Basic Windows PowerShell is an extensible command-line shell and associated Scripting language from Microsoft In Computing, regular expressions provide a concise and flexible means for identifying strings of text of interest such as particular characters words or patterns of characters
  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.

See also

References

  1. ^ Haskell 98 Language and Libraries: The Revised Report
  2. ^ American National Standard Programming Language FORTRAN (1978-04-03). A branch (or jump on some Computer architectures, such as the PDP-8 and Intel x86) is a point in a Computer program where the In Computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code ( method) at Runtime. Year 1978 ( MCMLXXVIII) was a Common year starting on Sunday (link displays the 1978 Gregorian calendar) Events 1043 - Edward the Confessor is crowned King of England. Retrieved on 2007-09-09. Year 2007 ( MMVII) was a Common year starting on Monday of the Gregorian calendar in the 21st century. Events 1000 - Battle of Svolder, Viking Age. 1379 - Treaty of Neuberg, splitting the Austrian
  3. ^ VisualWorks: Conditional Processing (2006-12-16). Year 2006 ( MMVI) was a Common year starting on Sunday of the Gregorian calendar. Events 755 - An Lushan revolts against Chancellor Yang Guozhong at Fanyang, initiating the An Shi Rebellion Retrieved on 2007-09-09. Year 2007 ( MMVII) was a Common year starting on Monday of the Gregorian calendar in the 21st century. Events 1000 - Battle of Svolder, Viking Age. 1379 - Treaty of Neuberg, splitting the Austrian

© 2009 citizendia.org; parts available under the terms of GNU Free Documentation License, from http://en.wikipedia.org
Dapyx Software network: MP3 Explorer | Ebook Manager | Zenithic