Branch (computer science)

A conditional statement is in programming a program section is executed only under a certain condition. A branch determines which is two ( or more) sections of the program, depending on one (or more) conditions performed.

Conditional statements and branches form, together with the loops, the control structures of programming languages. They are among the most important components of the programming because through them a program to respond to different states and inputs.

Definition

Conditional Statement

A conditional statement consists of a condition and a section of code, which in turn consists of one or more statements. If achieved the conditional statement in the program execution, then the condition is evaluated only and if this is true ( and only if) the section of code is then executed. Thereafter, in each case, the program execution continues with the following conditional assignment instructions. In pseudo-code written:

If condition then    Statement (s) end The condition is causal in nature and not to be confused with a temporal ( time ) condition, that is, it depends on whether the condition at the time, because the conditional statement is encountered during program execution, true or not.

Examples: in many programming languages ​​with C- like syntax (eg Java), conditional instructions are implemented as follows:

If ( temperature <20) {    Heating switched on (); } If the queried value here temperature is less than 20, the function of heating power on ( ) is executed. If the condition is not satisfied, so temperature is not less than 20, the instruction is skipped.

In SQL there is something like the following construct:

DELETE FROM table WHERE tabelle.id = 42; This corresponds to a loop that goes over the entries in a table and, for each table entry a Conditional statement is executed: all entries, for which the condition "id = 42" is true, will be deleted.

Branching

A branch (also called selection or selection ) consists of a condition and two code sections. Again, the only condition is evaluated, and if so, then the first section of code is executed, otherwise, the second section of code is executed:

If condition then    Statement (s) otherwise    Statement (s) end Example in C:

If ( temperature <20)    Heating switched on (); else    HeizungAusschalten (); selection operator

In some programming languages ​​there is the ternary conditional operator: (also known as a conditional expression ). This operator can be used for example for value assignments to variables, but also be part of more complex expressions. It processes the three parameters in the form of a condition? Expression1: expression2. First condition is evaluated. If the result is true, expression1 is evaluated, otherwise expr2. The resulting outcome is also the outcome of the selection operator. The following code shows the same function - once as an if - else construct and once as a shorthand notation.

Example in C:

Const char * x; / / if- else construct if ( number == 5 )    x = " number equal to 5"; else    x = " number not equal to 5";   / / With the selection operator: x = (number == 5 )? " Number equal to 5", " number not equal to 5"; Another example:

Int b, x;    x = (? b> 7 2 * b: 3 * b) 1; Multiple branching

In many programming languages ​​, there are also multiple branches, also known as case distinctions. Two forms can be distinguished: Either determines the result of an expression, which is executed by a plurality of code sections ( and if at all one is running it), or there are multiple conditions, each associated with a section of code. In both forms, an else part be present ( another section of code ).

First form

The expression is evaluated and compared with the value of information (in this case value1 to value3 ). If they match, the statements are executed according to the value specified:

If expression is equal to     Value1: statement (s)     Value2: statement (s)     Value3: statement (s)     else: statement (s) end Example in C:

Switch (number) {     case 0: v = 1; break; / / ' Break' is needed here, so that not even the ..     case 1: v = 2; break; / / Be executed .. following instructions (C- specialty)     case 2: v = 5; break;     default: v = 10; / / The else part } The exact semantics of such a construct is highly dependent on the particular programming language. So is not always allowed, for example, the else part, and sometimes he has to be even available. Sometimes the language definition also stipulates that there will be a runtime error if the else part is missing and the expression gets none of the indicated values ​​.

Second Form

The conditions are evaluated in order, until one matches. Then, the condition code associated with this section is performed, and the treatment of multiple branching is completed. If none of the conditions is true, the else part is executed if it is present:

If condition then    Statement (s) else if condition2 then    Statement (s) otherwise    Statement (s) end Example of the second form in PHP:

If ($ value == 1) {    First () function; } elseif ($ value == 2) {    Second function (); } else {    Other function (); } see also

Pictures of Branch (computer science)

111945
de