6502 Assembly has 8 conditional branch instructions; each instruction will test the appropriate flag and condition and jump between -128 and 127 bytes.
To understand these conditional instructions, it is helpful to remember that the comparison instructions (CMP, CPX, CPY) set the flags as if a subtraction had occurred:

LDA #10 CMP #11

Following these instructions, the accumulator will still hold 10 but the flags are set as if you had instructed the processor to perform 10 - 11.
The result is -1, so the sign flag will be set, the zero flag will be cleared, the overflow flag will be cleared, and the carry flag will be set.

BNE ;Branch on Not Equal - branch when the zero flag is set BEQ ;Branch on EQual - branch when the zero flag is set. ;The zero flag is set when the result of an operation is zero

BMI ;Branch on MInus BPL ;Branch on PLus - branch when the sign flag is cleared/set. ;The sign flag is set when the result of an instruction is a negative number ;and cleared when the result is a positive number

BVS ;Branch on oVerflow Set BVC ;Branch on oVerflow Cleared - branch when the overflow flag is cleared/set. ;The overflow flag is set when the result of an addition/subtraction would ;result in a number larger than 127 or smaller than -128

BCS ;Branch on Carry Set BCC ;Branch on Carry Clear - branch when the carry flag is cleared/set. ;The carry flag is set when an addition produced a carry and when ;a subtraction produced a borrow and cleared if an addition/subtraction ;does not produce a carry/borrow. The carry flag also holds bits ;after shifts and rotates.

In the following example, the branch will be taken if memory location Variable holds 200:

When there is no when others clause, the compiler will complain about any uncovered alternative. This defends against a common reason for bugs in other languages.
I.e., the following code is syntactically incorrect:

A selective entry call provides a way to time-out an entry call.
Without the time-out the calling task will suspend until the entry call is accepted.

select My_Task.Start;ordelay Timeout_Period;endselect;

The entry Start on the task My_Task will be called.
If My_Task accepts the entry call before the timer expires the timer is canceled. If the timeout expires before the entry call is accepted the entry call is canceled.

% if-then-else can also be used in an expression % write( if a < 4 then "lt 4" else "ge 4" );

% algol W also has a "case" statement, an integer expression is used to % % select the statement to execute. If the expression evaluates to 1, % % the first statement is executed, if 2, the second is executed etc. % % If the expression is less than 1 or greater than the number of % % statements, a run time error occurs % case a + b of begin write( "a + b is one" ) ; write( "a + b is two" ) ; write( "a + b is three" ) ; write( "a + b is four" ) end;

% there is also an expression form of the case: % write( case c - a of ( "one", "two", "three", "four" ) )

In AmbientTalk, if:then:else: is a keyworded message (as in Smalltalk).
The first argument should be a boolean expression.
The second and third arguments should be blocks (aka anonymous functions or thunks).

obj.foo() will not be executed if obj != null returns false. It is possible to have conditionals without short circuit evaluation using the & and | operators (from Bitwise operations). So in this conditional:

10 LET A%=1: REM A HAS A VALUE OF TRUE20 IF A% THEN PRINT "A IS TRUE"30 WE CAN OF COURSE USE EXPRESSIONS40 IF A%<>0 THEN PRINT "A IS TRUE"50 IF NOT(A%) THEN PRINT "A IS FALSE"60 REM SOME VERSIONS OF BASIC PROVIDE AN ELSE KEYWORD70 IF A% THEN PRINT "A IS TRUE" ELSE PRINT "A IS FALSE"

Here are code snippets from a more modern variant that does not need line numbers:

Befunge only has one conditional structure, which comes in two flavors: vertical IF ( | ) and horizontal IF ( _ ).
Befunge only has two boolean commands, greater-than ( ` ) and not ( ! ).
These snippets input a number and use the conditional operators to print a "0" if it is zero and an "X" otherwise.

v > "X",@ non-zero> & | > "0",@ zero

# is the skip command.
It unconditionally skips one character, allowing a little flexibility in flow control.

Bracmat uses & and | for branching.
These binary operators are like && and || in C-like languages.
Bracmat does not have the notion of Boolean variables, but marks all evaluated expressions as either succeeded or failed.
If the left hand side of the & operator has succeeded, Bracmat goes on evaluating the right hand side.
Only if both of left and right hand sides succeed, the expression tree headed by the & operator as a whole succeeds. Likewise, only if both of left and right hand sides of an expression tree headed by | fail, the expression tree as a whole fails.
Evaluated expressions are just that: expressions.
The following expression writes "That's what I thought." to your screen and evaluates to the expression "Right".

2+2:5 & put$"Strange, must check that Bracmat interpreter." & 0| put$"That's what I thought." & Right

Use a patterns with alternations. Note that the match-expression (the tree headed by the : operator) evaluates to the left hand side of the : operator.
In the following example, the resulting expression is a single node containing "4".

Brainf*** has two conditional jump instructions, [ and ]. the [ instruction jumps forward to the corresponding ] instruction if the value at the current memory cell is zero, while the ] instruction jumps back if the current memory cell is nonzero.
Thus in the following sequence:

The (if ...) construct takes a predicate as its first argument and evaluates it.
Should the result be non-nil, it goes on to evaluate and returnm the results of the 'then' part, otherwise, when present, it evaluates and returns the result of the 'else' part. Should there be no 'else' part, it returns nil.

(if(= val 42)"That is the answer to life, the universe and everything""Try again"); the else clause here is optional

The only conditional operation provided is BRZ (branch on accumulator negative). For an example illustrating how this instruction can be used to code "equal to", "greater than", and "less than", see Integer comparison#Computer/zero Assembly.

switch(value){case1:// Some taskbreak;// Breaks are required in C#.case2:case3:// Some taskbreak;default:// If no other case is matched.// Some taskbreak;}

If fall through algorithms are required use the goto keyword.

switch(value){case1:// Some taskgotocase2;// will cause the code indicated in case 2 to be executed.case2:// Some taskbreak;case3:// Some taskbreak;default:// If no other case is matched.// Some taskbreak;}

// "final switch" is safer, for enums (and in future other values,// like Algebraic), because all cases must be present.// with() is handy to avoid repeating "Colors." for each case.finalswitch(c)with(Colors){case yellow:break;case blue:break;case brown, green:break;// case yellow: .. case brown: // Forbidden in final switches.// default: // Forbidden in final switches.}}

Erlang's conditionals are based on pattern matching and guards.
There are several mechanisms for this: case-of, if, function clauses.
Pattern matching allows destructuring a term and matches a clause based on the structure.
In the case example the term is X and the pattern is {N,M} or _. _ will match anything, while {N,M} will only match tuples of two terms.
Though N and M could be any other type (in this case an error will occur if they're non-numeric).
Guards allow more specification on the terms from the matched pattern. In the case example comparing N and M are guards.

There are many conditional structures in Factor.
Here I'll demonstrate the most common ones.
A few of these have other variations that abstract common stack shuffle patterns.
I will not be demonstrating them.

In ISO Fortran 90 and later, there are three conditional structures. There are also a number of other *unstructured* conditional statements, all of which are old and many of which are marked as "deprecated" in modern Fortran standards. These examples will, as requested, only cover conditional *structures*:

ISO Fortran 90 and later has a concurrent, array-expression-based WHERE-ELSEWHERE structure. The logical expressions in WHERE and ELSEWHERE clauses must be array-values. All statements inside the structure blocks must be array-valued. Furthermore, all array-valued expressions and statements must have the same "shape". That is, they must have the same number of dimensions, and each expression/statement must have the same sizes in corresponding dimensions as each other expression/statement. For each block, wherever the logical expression is true, the corresponding elements of the array expressions/statements are evaluated/executed.

case statements take wildcards as arguments, but because of syntax quirk, they have to be quoted (just like in Powershell), otherwise they would match files in current directory. Unlike switch statements in C, they don't fall through. To match something that would be matched if nothing was matches use wildcard that matches everything, the language doesn't have default statement.

switch
While if-elseif-else-endif and do case constructions allows using of any expressions as conditions, the switch allows literals only in conditional case statements. The advantage of the switch command is that it is much faster.

obj.foo() will not be executed if obj != null returns false. It is possible to have conditionals without short circuit evaluation using the & and | operators (from Bitwise operations). So in this conditional:

This structure will only work if the code being switched on evaluates to an integer or character. There is no switching on Objects or floating-point types in Java (except for Strings in Java 7 and higher).

switch(c){case'a': foo();break;case'b': bar();default: foobar();}

This particular example can show the "fallthrough" behavior of a switch statement. If c is the character b, then bar() and foobar() will both be called. If c is the character a, only foo() will be called because of the break statement at the end of that case.

Also, the switch statement can be easily translated into an if-else if-else statement. The example above is equivalent to:

The distinctive feature of the ternary operator (compared to JavaScript's other conditional structures) is that it evaluates as an expression rather than a statement, and can therefore be composed within larger expressions, making it a valuable resource of program structure in a functional idiom of JavaScript.

Notice that if cond produces a nonempty stream, then the entire expression will typically do the same. Since f and g also can produce streams, this lends itself to interesting Cartesian-product possibilities.
There is no "case <exp>" construct, but the idiom illustrated by the following example can be used to avoid the need to create a temporary variable to hold the "case" expression:

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

The LC3 sets condition codes (N[egative], Z[ero], and/or P[ositive]) based on the results of instructions that write values into the general purpose registers. The BR instruction utilizes these condition codes are to branch conditionally. If the BR instruction specifies one or more condition codes and at least one specified code is set, then the PC will be updated to the branch address. If none of the specified condition codes is set, then the next sequential instruction will execute. If the BR instruction does not specify any condition codes, then it is an unconditional branch, so the branch will be taken.

; or any combination of these condition codes, e.g.BRnz ; branch if (result <= 0)

The effect of if (x == y) { go to LABEL } is achieved by adding x to -y (the two's complements of y) and branching if the result is zero. The following example prints "Branch Taken!" because the values of x and y are both 1.

All standard versions of MUMPS allow a ELSE command, which can be abbreviated to E. Instead of depending on the previous IF command, the ELSE command depends on the value of the system variable $TEST. $TEST is set whenever an IF command is executed, and whenever a timeout is specified. Since $TEST could be changed and not noticed by an unwary programmer it is important to remember when writing code.
For example with the code:

IF T DO SUBROUTINE ELSE DO SOMETHING

It isn't clear whether $TEST is changed or not, because the function SUBROUTINE might change the value of $TEST by using a timeout or an IF command.

It is better to explicitly set the $TEST special variable using IF 1 for example:

IF T DO SUBROUTINE IF 1 ELSE DO SOMETHING

Another common practice is to use the argumentless DO, as it pushes the $TEST variable onto a stack and replaces it after the "dot block" is complete. An example of this code is:

The $Select statement contains couplets separated by commas, which each consist of a conditional test, followed by a colon, and what to return if that condition is true.
The first part of the couplet must be a truth value. Since only zero is interpreted a truth value of false, any nonzero numbers when interpreted as a truth value will be considered to be true. Typically the number 1 is used as an explicitly true condition and is placed in the final couplet. If no conditions are true, the program's error processing is invoked. The very first condition that is true is the result of the expression. In the example, the value will always be "Unequal" as it is always true, and the rest of the $SELECT will never be used.

Most commands can take a "postconditional", which is a colon and some conditional statement immediately after the command followed by the command separator (space) and the usual arguments of the command. The command is executed only if the conditional statement evaluates to true.

The exceptions are FOR, IF, and ELSE.
There are several commands that also allow for post-conditionals in their arguments.
The GOTO, and DO commands must have a label but it optionally have a colon followed by a truth value. When the truth value is interpreted as false, the flow of control does NOT move to the label indicated. If it is true, then flow of control does move to the label.
Similarly, the XECUTE command may have a colon and postcondition on its argument, which is a expression that is interpreted as a line of MUMPS code. That code is executed when the postcondition is true, and not executed when it is false.
Some people consider timeouts to be a form of conditional.
For example in the READ command, a number (or numeric expression) after a colon is the number of seconds to wait for a user to make an entry. If the user doesn't make an entry before the timeout, the special variable $TEST is set to 0 (zero), indicating a timeout has occurred.
Likewise in the JOB command, a number (or numeric expression) after a colon is the number of seconds to wait for the system to start a new job running in "parallel" to the current job. If the system does not create a new job before the timeout, the special variable $TEST is set to 0 (zero), indicating a timeout has occurred.

if (cond) <then> else <this>; is an expression in Nemerle, requiring both keywords (if and else) to be valid. when and unless are macros for which <this> = null. cond must be an expression that evaluates to a bool (true|false), other types aren't automatically assigned truth or falsehood as in some languages.

For all of the conditional instructions, the conditional expression must evaluate either to '1' or '0'. Note that ooRexx conditional
expression evaluation does not have a short circuiting mechanism. Where the logical operations | (or), & (and), or && (exclusive or) are
used, all parts of the expression are evaluated.
The conditional
may also be a list of conditional expressions separated by commas. The expressions are evaluated left-to-right, and evaluation
will stop with the first '0' result. For example,

ifarg~isa(.string)&arg~left(1) == "*"thencall processArg arg

would fail with a syntax error if the variable arg does not hold a string because the right-hand-side of the expression
is still evaluated. This can be coded as

ifarg~isa(.string), arg~left(1) == "*"thencall processArg arg

With this form, the second conditional expression is only evaluated if the first expression is true.

Case selectors must be an ordinal type. This might seem to be a restriction, but with a little thought just about anything can be resolved to an ordinal type. Additionally, each selector may consist of more then one item. The optional ELSE keyword provides a default for values that do not match any of the given cases.

In Pascal there is no fall-through to the next case. When execution reaches the end of a matching clause, it continues after the end of the case statement, not in the code for the next case.

case i of1,4,9:{ executed if i is 1, 4 or 9 } DoSomething;11,13..17:{ executed if i is 11, 13, 14, 15, 16 or 17 } DoSomethingElse;42:{ executed only if i is 42 } DoSomeOtherThing;else DoYetAnotherThing;end;

The ternary operator is used as an expression within a statement, rather than as a control flow structure containing one or more statements. It is frequently used in assignment, or sometimes for passing function call arguments that vary depending on some condition.

At first there was no switch structure in Perl, although there were plenty ways to emulate it. In Perl 5.8, an experimental switch/case/else structure was introduced. Perl 5.10 replaced this with the given/when/default structure borrowed from Perl 6.

when blocks are allowed in any block that topicalizes $_, including a
for loop (assuming one of its loop variables is bound to $_)
or the body of a method (if you have declared the invocant as $_)." See Synopsis 4.

(if2 (condition1) (condition2) # If both conditions evaluate to non-NIL (expression-both) # Then execute this expression (expression-first) # Otherwise this for the first (expression-second) # or this the second condition. (expression-none) # If both are NIL, all following expressions (and-more) )

A "pure" Prolog program by its very nature is one very long, very complicated boolean test. Absolutely every executable portion of Prolog is a test that succeeds or fails. Here are some examples, thus, of using conditionals in Prolog:

While operationally this looks like a program that when go/0 is executed will print "Hello, World!" and exit, it is actually a predicate, in the strict logical sense of the term, that tests conditions. Denotationally we'd describe it as "go/0 succeeds iff write/1 succeeds with its passed-in argument, and if nl/0 subsequently succeeds." (The fact that write/1 and nl/0 **always** succeed and that we use them for their side effects only doesn't matter to the Prolog view of a program.)

fact(foo).fact(bar).fact(baz).

go :- fact(booger).go :- fact(bar).

This example shows a few features of Prolog's testing and, specifically, shows nondeterminism and backtracking in action. In this we have a predicate fact/1 (so named because in this format, without an executable body, it is termed a "fact" in the literature). It has two clauses asserting both "bar" and "baz" as facts. go/0 also has two clauses. If we execute go/0, the runtime will tell us "true" (or, in some implementations, "yes") to indicate that the predicate call was successful. Denotationally we would say "fact(X) succeeds iff X unifies with foo, X unifies with bar, or X unifies with baz". We would also say "go/0 succeeds iff fact(booger) succeeds or if fact(bar) succeeds". When running, the first clause of go/0 will be executed and fact(booger) will be tested. fact(booger) does not match fact(bar) nor does it match fact(baz) so it fails. This leads the runtime to go back and try again with the **second** go/0 clause. In this one fact(bar) does, in fact, match fact(bar), so the overall test passes. A Prolog program is, thus, a very complicated tree of if/then statements, in effect.

fact(X):-( X = foo; X = bar; X = baz ).

go :-( fact(booger); fact(bar)).

This version is semantically the same as the previous one. (In actual execution, because of some runtime optimizations, there are some minor differences in outcome, but nothing that would change the logical interpretation of the program.) Here we're showing more explicitly the various "or" conditions. In Prolog "," is roughly equivalent to "and" (conjunction) while ";" is roughly equivalent to "or" (disjunction). Because of this, and because of the fact we've taken separate clauses now and put them into explicit disjunctions it is clearer that we're performing a series of if/then tests in effect.

That being said, Prolog does have something that's very akin to real if/then statements (or, more accurately, similar to the ternary operator of languages like C):

In this version of fact/1, the -> operator is used to perform a more traditional if/then/else. The general construct is ( condition -> succeed_branch ; fail_branch ). In this case if the parameter passed in unifies with 'bar', a message is written (recall that write/1 and nl/0 always succeed!) and the whole predicate exists with a success. If, on the other hand, the unification fails (you pass anything other than 'bar') it writes a snarky message and then calls fail/0, a predicate that, as its name suggests, always fails. There are more implications to using the conditional expression in Prolog; it is generally considered code smell. Other operators also exist for handling conditionals (like *->) that lack the "smell" of the conditional operator. The reasons for this are out of scope, however, for this article. Just know that the fact/1 predicate could have used *-> in place of -> and been more "sound" as a result.

Compiler conditional structures works like normal conditional structures, except they are evaluated at compile time, and thus have to use constant expressions. Any defined constant can be used, these examples uses built-in constants.

In some cases it's useful to associate functions with keys in a dictionary; and simply use this in lieu of long sequences of "if...elif...elif..." statements.

dispatcher =dict()dispatcher[0]=foo # Not foo(): we bind the dictionary entry to the function's object,# NOT to the results returned by an invocation of the functiondispatcher[1]=bardispatcher[2]=baz # foo,bar, baz, and boz are defined functions.

# Then laterresults = dispatcher.get(x, boz)()# binding results to a name is optional# or with no "default" case:if x in dispatcher: results=dispatcher[x]()

case i of when {1, 4, 9}: # Executed if i is 1, 4 or 9 statement1; when {11} | {13 .. 17}: # Executed if i is 11, 13, 14, 15, 16 or 17 statement2; when {42}: # Executed only if i is 42 statement3; otherwise: statement4;end case;

The SSEM's only conditional operation is 011 Test, which causes the computer to skip the next instruction if the value held in the accumulator is negative. This program illustrates it: assuming address 10 stores a variable, we test whether its negation is negative (i.e. whether the variable itself is positive). If it is, we skip the next instruction and proceed with the program; but, if it is not negative (i.e. the variable is negative or zero), we jump to address 1 + the value stored at address 14. It is easy to see how this can be used to implement loops, other conditional tests, etc.

In TXR, most directives are conditionals, because they specify some kind of match. Given some directive D, the underlying logic in the language is, roughtly, "if D does not match at the current position in the input, then fail, otherwise the input advances according to the semantics of D".

An easy analogy to regular expressions may be drawn. The regex /abc/ means something like "if a doesn't match, then fail, otherwise consume a character and if b doesn't match, then fail, otherwise consume another character and if c doesn't match, then fail otherwise consume another character and succeed." The expressive power comes from, in part, not having to write all these decisions and book-keeping.

The interesting conditional-like structures in TXR are the parallel directives, which apply separate clauses to the same input, and then integrate the results in various ways.

For instance the choose construct will select, from among those clauses which match successfully, the one which maximizes or minimizes the length of an extracted variable binding:

If any subclause fails to match, then all stops processing subsequent clauses. There are subtleties though, because an earlier clause can produce variable bindings which are visible to later clauses.
If previously bound variable is bound again, it must be to an identical piece of text:

@# match a line which contains some piece of text x@# after the rightmost occurence of : such that the same piece@# of text also occurs at the start of the line preceded by -->@(all)@*junk:@x@(and)-->@x@/.*/@(end)

Wrapl's goal directed evaluation can be used to control conditional execution.
The select-right operator & produces the values of the right operand for each value produced by the left operand. Thus if the left operand fails to produce any values, the right operand is never evaluated.

condition & success

The sequence operator | produces the values of the left operand followed by the values of the right operand. Thus if the left operand produces enough values (for example in a context where only one value is required), the right operand is never evaluated.

Assembly doesn't work on if/else if/else statements(Unless you're using MASM or alike assemblers:)). Rather, it has conditional jumps which work off flags set by the comparison. Take this general statement from C.

if(i>1) DoSomething

FailedSoContinueCodeExecution.

There are actually a number of ways to implement that in assembly. The most typical way would be something like..

cmp i,1jg _DoSomethingFailedSoContinueCodeExecution

Using the "jg" instruction,our code will jump to _DoSomething if the comparison(cmp i,1) made our ZF(ZeroFlag) flag well, zero. Which means only 1 thing. It is in fact greater than. In contrast, if i is in fact equal or less than 1, ZF is set to 1. The Zero Flag will remain set as long as we don't use any instructions that alter flags(comparisons for example). So, here's another C example

This does another state check on the Zero flag(actually jg/jle also check another flag, but that's not overly important) using jle. JumpifLessthanorEqual. Essentially, jle jumps if ZG is set to 1. So, it's jump condition is the opposite to jg.

One last commonly used condition.

if(i==1) DoSomethingelse DoSomethingElseFailedSoContinueExecution

In this case, we'd do this.

cmp i,1je _DoSomethingjne _DoSomethingElseFailedSoContinueExecution

The je/jne jump instructions are again like jg/jle opposites of each other and again like je/jne rely on how the zero flag is set in the previous comparison.
There are many different conditional jumps in assembly and many ways to set them, test, and, or to name a few. The ones covered are just some commonly used ones in order to show how assembly deals with conditional statements.