# (Introduction to C + + ---4) if else switch

if statement divides the program flow into two branches according to the judgment result of a condition.

• If else nesting
• switch statement

If else nesting

The first implementation method of multi branch is to use if else nesting, that is, to nest if else statements in if branch and / or else branch.

For example:

```cin >> angle;     // Enter angle
if (angle % 90 == 0)
{
// If else nesting
if (angle % 180 == 0)
cout << "Segment in X On shaft" << endl;
else
cout << "Segment in Y On shaft" << endl;
}
else
cout << "Segment in quadrant" << angle / 90 + 1 << endl;
```

The program calculates which quadrant or axis the input angle angle falls on. When the input angle is a multiple of 90 (angle% 90 = = 0 is true), there are two cases, either on the X-axis or on the Y-axis, so it needs to be judged again under other conditions (angle% 180 = = 0).

The above program implements three branches through if else nesting.

switch statement
Another way to realize multi-channel branching is to use the switch statement. The format is as follows:

```switch(<Conditional expression>)
{
case <Constant expression 1>:
<Statement sequence 1>;
case <Constant expression 2>:
<Statement sequence 2>;
......
case <Constant expression n>:
<Statement sequence n>;
default:
<Statement sequence n+1>;
}
```

Among them, switch, case and default are all C + + keywords< Conditional expression > is an expression with an integer value; Each case corresponds to a branch processing; The default branch is the default processing branch< Constant expressions 1 >... < constant expressions n > are all expressions whose values are integer constants< Statement sequence 1 >... < statement sequence n + 1 > is a group of statements and can be empty.

Execution process

The execution process of switch statement is as follows:

When executing a switch statement, first calculate < conditional expression > to get an integer value, compare the value with the values of < constant expression 1 >... < constant expression n > one by one, and if it is equal to one of them, execute the statement sequence under the constant expression.

It should be noted that after executing the statement sequence corresponding to the constant expression, the processing statement sequence of subsequent branches will continue to be executed until the switch statement ends or a jump instruction (break) is encountered; If the value of the test expression is not equal to the value of any constant expression, the statement after the default branch is executed.

switch statement and break

When a case branch condition is satisfied, after executing the statement sequence of the branch, the processing statement sequence of subsequent branches will continue to be executed. If you want to end the entire switch statement after executing a branch, you can add a break statement after each branch statement.

Break statement is a transfer statement, which can only appear in switch structure and loop structure. The break statement can jump out of the switch structure or loop structure directly containing the break statement (only one layer can jump out), and the program control can leave the switch structure or loop structure and execute its subsequent statements.

The switch statement mode with break statement is as follows:

```switch(<Conditional expression>)
{
case <Constant expression 1>:
<Statement sequence 1>;
break;
case <Constant expression 2>:
<Statement sequence 2>;
break;
......
case <Constant expression n>:
<Statement sequence n>;
break;
default:
<Statement sequence n+1>;
}
```

The execution process is as follows: first, calculate the < conditional expression >, compare the obtained integer value with the value of < constant expression 1 >... < constant expression n > one by one, once it is detected that it is equal to one of them, execute the statement sequence under the constant expression, and then execute the break statement to leave the switch structure, The processing statement sequence of subsequent branches is no longer executed, but the subsequent statements of the whole switch statement are executed.

For example, the following program converts the percentile system to the decimal output. If 85 is input and scorephase is calculated to be 8, the switch statement will be executed and the case 8: branch will be entered, B will be output, and then break will be executed; Statement jumps out of the entire switch statement:

```cin >> score;     // Enter the percentile score
scorePhrase = score / 10;     // Calculate fractions and convert them to decimals
// Judge and output level
switch (scorePhrase)
{
case 10:
case 9:
cout << 'A' << endl;
break;
case 8:
cout << 'B' << endl;
break;
case 7:
cout << 'C' << endl;
break;
case 6:
cout << 'D' << endl;
break;
case 0: case 1: case 2: case 3: case 4: case 5:
cout << 'E' << endl;
break;
default:
cout << "The score is illegal!" << endl;
}
```

Procedure: determine the day of the current year by the entered date (the data is provided by the platform and expressed in the form of "mm / DD / yyyy", which needs to be used after you obtain it). Specific requirements are as follows:

For inputting a date (with a space between month, year and day), such as June 15, 2017, calculate the day of the year and output it;

The output form is "Year Month Day is the X day" (where X is your calculation result).

Tip: the calculation idea of this problem is relatively clear. For example, on June 15, you need to add all the days of the first five months plus 15, and on May 3, you only need to add all the days of the first four months plus 3. Therefore, the calculation method of this problem is different according to different months, and leap years need to be considered.

Test input:

```2017 6 15
```

Expected output:

```2017-6-15 It's Day 166
```

Test input:

```2000 10 1
```

Expected output:

```2000-10-1 It's Day 275
```
```// Contains two I/O libraries, which can use either input or output mode
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
// y-year, m-month, d-Day, n-day
int y, m, d, n;

cin >> y >> m >> d >> n;
int array_m[] = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i = 0; i < m-1; i++)
{
n += array_m[i];
}

if(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
{
n += 1;
}

cout << y << "-" << m << "-" << d << "Yes" << n+d << "day" << endl;

return 0;
}
```

Tags: C C++ C# Algorithm

Posted on Fri, 19 Nov 2021 09:17:14 -0500 by Bobby_Jo