Skip to content

Branching

A program executes statements sequentially by default. Sometimes we need to selectively execute certain statements, which requires the use of branching constructs. Choosing appropriate branching statements can improve program efficiency.

if Statement

Basic if Statement

The following is the structure of a basic if statement.

1
2
3
if (condition) {
  body;
}

The if statement evaluates the condition; if the result is true (non-zero), the body is executed; otherwise, it is not.

If the body contains only a single statement, the curly braces can be omitted.

if...else Statement

1
2
3
4
5
if (condition) {
  body1;
} else {
  body2;
}

The if...else statement is similar to the if statement; the else does not require a condition. When the condition of the if statement is satisfied, the statements inside the if are executed; when the condition is not satisfied, the statements inside the else are executed. Similarly, if the body contains only one statement, the curly braces can be omitted.

else if Statement

1
2
3
4
5
6
7
8
9
if (condition1) {
  body1;
} else if (condition2) {
  body2;
} else if (condition3) {
  body3;
} else {
  body4;
}

The else if statement is a combination of if and else, evaluating multiple conditions and selecting different statement branches. The final else statement does not require a condition. For example, if condition1 is true, body1 is executed; if condition3 is true while condition1 and condition2 are false, body3 is executed; body4 is executed only when all conditions are false.

In fact, this statement is equivalent to having only an if statement in the else clause of the first if, with the curly braces omitted and written together. If the conditions are mutually exclusive, writing this way can make the code logic clearer.

Logically, it roughly corresponds to the following passage:

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

  • If (\(\Delta<0\)) the equation has no real roots;
  • Else if (\(\Delta=0\)) the equation has two identical real roots;
  • Else the equation has two distinct real roots;

switch Statement

1
2
3
4
5
6
7
8
switch (selector) {
  case label1:
    body1;
  case label2:
    body2;
  default:
    body3;
}

When a switch statement is executed, the value of the selector is first evaluated. Then, based on this value, the corresponding label is selected, and execution starts from that label. Here, the selector must be an expression of integer type, and all labels must be integer constants. For example:

1
2
3
4
5
6
int i = 1;  // Here i is of integer type, satisfying the requirement for an integer expression

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

1
2
3
4
5
6
7
8
char i = 'A';

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

In a switch statement, break statements must be added as needed to interrupt execution; otherwise, after the corresponding case is selected, all statements in the following cases and the default case will be executed. See the example below for details.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
char i = 'B';

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

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

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

The output of the above code will be WIKI and Hello World. If you do not want the statements in subsequent branches to be executed, a break is needed. See the example below for details.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
char i = 'B';

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

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

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

The output of the above code will be WIKI. Because of the break, the following statements will not be executed. The last statement does not need a break because there are no statements after it.

Case labels must not be duplicated, but their order can be reversed. That is, the order of case labels is not important. The order of appearance of each case (including default) can be arbitrary. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
char i = 'B';

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

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

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

Curly braces can also be optionally added in the case clauses of a switch. However, note that if you need to define variables inside a switch statement, curly braces are mandatory. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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

In the above text, terms like "case clause" and "case subclause" are used frequently. In fact, at the underlying implementation level, switch is equivalent to a set of jump statements. Consequently, there are tricks like Duff's Device; those interested can learn about it on their own.