Branching

By default, a program executes its code in order. Sometimes we need to execute certain statements selectively; this is where branching is used. Choosing suitable branch statements can improve program efficiency.

if statement

Basic if statement

The structure of a basic if statement is as follows.

if (condition) {
  body;
}

An if statement evaluates the condition. If the result is true (nonzero), it executes the statement; otherwise, it does not.

If the body contains only one statement, the braces may be omitted.

if...else statement

if (condition) {
  statement_1;
} else {
  statement_2;
}

An if...else statement is similar to an if statement, except that else does not require another condition. When the condition of the if statement is satisfied, the statements in if are executed; when it is not satisfied, the statements in else are executed. Likewise, when the body contains only one statement, the braces may be omitted.

else if statement

if (condition_1) {
  statement_1;
} else if (condition_2) {
  statement_2;
} else if (condition_3) {
  statement_3;
} else {
  statement_4;
}

An else if statement combines if and else to test multiple conditions and choose different statement branches. The final else statement does not require another condition. For example, if condition 1 is true, body 1 is executed; if condition 3 is true while conditions 1 and 2 are both false, body 3 is executed; body 4 is executed only when all conditions are false.

In fact, this structure is equivalent to placing a single if statement in the else clause of the first if and omitting the braces. When the conditions are mutually exclusive, this form makes the code's logic clearer.

Logically, it is roughly equivalent to the following:

When solving a quadratic equation, the relationship between its roots and discriminant is:

  • If (Δ<0\Delta<0), the equation has no solution;
  • Otherwise, if (Δ=0\Delta=0), the equation has two equal real solutions;
  • Otherwise, the equation has two distinct real solutions;

switch statement

switch (selector) {
  case case_label_1:
    statement_1;
  case case_label_2:
    statement_2;
  default:
    statement_3;
}

When a switch statement executes, it first evaluates the selection expression and then selects the corresponding label according to its value, beginning execution at that label. The selection expression must be an integer-type expression, and every label must be an integer constant. For example:

int i = 1;  // The data type of i is integer, satisfying the requirement for an integer-type expression

switch (i) {
  case 1:
    cout << "OI WIKI" << endl;
}
char i = 'A';

// The data type of i is character, but char
// is also an integer type, satisfying the requirement for an integer-type expression
switch (i) {
  case 'A':
    cout << "OI WIKI" << endl;
}

A switch statement also needs break statements as required to interrupt execution. Otherwise, after the corresponding case is selected, the statements in all subsequent cases and in default are run. See the examples below.

char i = 'B';

switch (i) {
  case 'A':
    cout << "OI" << endl;
    break;

  case 'B':
    cout << "WIKI" << endl;

  default:
    cout << "Hello World" << endl;
}

After the code above runs, its output is WIKI and Hello World. If you do not want the statements in the following branch to run, a break is needed; see the example below.

char i = 'B';

switch (i) {
  case 'A':
    cout << "OI" << endl;
    break;

  case 'B':
    cout << "WIKI" << endl;
    break;

  default:
    cout << "Hello World" << endl;
}

After the code above runs, its output is WIKI. Because break is present, the following statements are not executed. The final statement does not need break because there are no statements after it.

Case labels cannot be repeated, but their order can be reversed. In other words, the order of case labels does not matter. The order in which the cases (including default) appear can be arbitrary. For example:

char i = 'B';

switch (i) {
  case 'B':
    cout << "WIKI" << endl;
    break;

  default:
    cout << "Hello World" << endl;
    break;

  case 'A':
    cout << "OI" << endl;
}

Braces may also be added selectively to case clauses in a switch statement. Note, however, that braces are required if a variable needs to be defined inside a switch statement. For example:

char i = 'B';

switch (i) {
  case 'A': {
    int i = 1, j = 2;
    cout << "OI" << endl;
    ans = i + j;
    break;
  }

  case 'B': {
    int qwq = 3;
    cout << "WIKI" << endl;
    ans = qwq * qwq;
    break;
  }

  default: {
    cout << "Hello World" << endl;
  }
}
Understanding switch

The terms “case branch” and “case clause” were used extensively above. In the underlying implementation, switch is essentially a group of jump statements. This is also why tricks such as Duff's Device exist; interested readers can study it independently.

This content comes from OI Wiki: Branching. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.