Conditional statement and branch

from Wikipedia, the free encyclopedia
Activity diagram of an if-else statement

In programming, a conditional instruction is a program section that is only executed under certain conditions. A branch determines which of two or more program sections, depending on one or more conditions, is executed.

Conditional statements and branches , together with the loops , form the control structures of the programming languages . They are one of the most important components of programming , as they allow a program to react 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 the conditional statement is reached during program execution, then the condition is evaluated first, and if this applies and only then is the code section subsequently executed. After that, program execution is continued in any case with the statements following the conditional statement. Written in pseudocode :

falls Bedingung dann
  Anweisung(en)
ende

The condition is causal in nature and not to be confused with a temporal condition, i. In other words, it depends on whether the condition applies or not at the time when the conditional statement is reached during the program run.

Examples: In many programming languages with C -like syntax, e.g. B. C ++ , C # and Java , conditional statements are implemented as follows:

if (Temperatur < 20)
{
    HeizungEinschalten();
}

If the value requested here is Temperaturless than 20, the function is HeizungEinschalten()carried out. If the condition is not met, i.e. Temperatur not less than 20, the instruction is skipped.

The following construct exists in the database language SQL :

delete from tabelle where tabelle.id=42;

This corresponds to a loop that goes over the entries in a table and in which a conditional statement is executed for each table entry: all entries for which the condition “id = 42” applies are deleted.

branch

A branch, even selection or selection called, consists of a condition code and two sections. Again, the condition is evaluated first, and if it is true, the first code section is then executed, otherwise the second code section is executed:

falls Bedingung dann
  Anweisung(en)
sonst
  Anweisung(en)
ende

Example in the programming language C :

if (Temperatur < 20)
{
    HeizungEinschalten();
}
else
{
    HeizungAusschalten();
}

Selection operator

In some programming languages there is the ternary selection operator?, Which is also called a conditional expression . This operator can be used, for example, to assign values to variables , but it can also be part of more complex expressions. It processes three parameters in the form of condition? Expression1: Expression2 . First condition evaluated. If the result is true , expression1 is evaluated; otherwise, expression2 . The resulting result is also the result of the selection operator. The following code shows the same function - once as an if-else construct and once as a short form.

Example in the programming language C :

const char *x;
// if-else-Anweisung
if (zahl == 5)
{
    x = "Zahl gleich 5";
}
else
{
    x = "Zahl ungleich 5";
}

// Mit Auswahloperator:
x = zahl == 5 ? "Zahl gleich 5" : "Zahl ungleich 5";

Another example:

  int x;
  x = (b > 7 ? 2 * b : 3 * b) + 1;

Multiple branching

In many programming languages there are also multiple branches, also called case distinctions . A distinction must be made between two forms: Either the result of an expression determines which code section is executed and whether one of them is executed at all, or there are several conditions, each of which is assigned a code section. There can be an else part in both forms .

First form

The expression is evaluated and compared with the value specifications (here value1 to value3). If they match, the statements are executed after the value specified. If no value matches, the statements according to else are executed if the else part is present:

falls Ausdruck gleich
   Wert1: Anweisung(en)
   Wert2: Anweisung(en)
   Wert3: Anweisung(en)
   sonst: Anweisung(en)
ende

Example in the programming language C :

switch (zahl)
{
    case 0:
        v = 1;
        break; // 'break' ist hier nötig, damit nicht auch die ...
    case 1:
        v = 2;
        break; // ... folgenden Anweisungen ausgeführt werden (Besonderheit in C)
    case 2:
        v = 5;
        break;
    default:
        v = 10; // der sonst-Teil
}

The following example in the programming language C # checks the season of the year and outputs a line of text:

int month = date.Month;
string season;
switch (month)
{
	case 3:
	case 4:
	case 5:
		season = "spring";
		break;
	case 6:
	case 7:
	case 8:
		season = "summer";
		break;
	case 9:
	case 10:
	case 11:
		season = "autumn";
		break;
	case 12:
	case 1:
	case 2:
		season = "winter";
		break;
	default:
		season = "unknown";
		break;
}
Console.WriteLine("The date is in " + season + ".");

The exact semantics of such a construct strongly depends on the programming language . So is z. B. the else part is not always allowed, but sometimes it has to be present. Sometimes the language definition also stipulates that a runtime error occurs if the else part is missing and the expression does not take any of the specified values.

Second form

Data flow diagram of a nested conditional if-else statement

The conditions are evaluated in sequence until one of them applies. Then the code section belonging to this condition is executed and the handling of the multiple branch is thus ended. If none of the conditions are met, the else part is executed if it is present:

falls Bedingung dann
  Anweisung(en)
sonst falls Bedingung2 dann
  Anweisung(en)
sonst
  Anweisung(en)
ende

The following method in the C # programming language checks whether a point in time is before, after or in a given period and outputs a line of text:

void Compare(DateTime time, DateTime startTime, DateTime endTime)
{
	if (time < startTime)
	{
		Console.WriteLine("The point in time is before the period.");
	}
	else // time >= startTime
	{
		if (time > endTime)
		{
			Console.WriteLine("The point in time is after the period.");
		}
		else // time >= startTime && time <= endTime
		{
			Console.WriteLine("The point in time is in the period.");
		}
	}
}

See also