"Dou Po CPP" Chapter II (Part 1) ---- first acquaintance branch sentence

Let me give you a preview: The second and third chapters of "Dou Po CPP" will be divided into two parts to share with you . .

content

if statement

if statement (first)

if statement (second)

Nested if statements

Broken!----Syntax of if statement

plan 1

Scenario 2

switch statement

The magic of switch

Label

break statement

default tag

Communication between if statement and switch statement

To be continued


if statement

if statement (first)

Let's start with an appetizer that leads to the if statement:

//chap0201

#include <iostream>  
        
using namespace std;

int main()
{
	int n;

	cout << "整数值:";
	cin >> n;

	if (n > 0)
		cout << "这个值为正。\n";
}

The statement that appears in the above code to determine whether n is greater than 0 is an if statement . Its syntax is as follows:

if (condition) statement

The statement is executed only if the condition inside ( ) is true and the expression is true. This obviously has many limitations .

The condition of this program is n>0. The > operator returns true if the value of the left operand is greater than the right operand, and false otherwise.

true and flase are constant values ​​of type bool and become boolean literals .

The flow chart of chap0201 is as follows:

If the input value of n is less than or equal to 0, this statement will not be executed and nothing will be displayed on the screen.

if statement (second)

Let's optimize the chap0201 code:

//chap0202

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout << "整数值:";
	cin >> n;

	if (n > 0)
		cout << "这个值为正。\n";
	else
		cout << "这个值为0或负。\n";
}

The syntax of this if statement is as follows:

if (condition) statement else statement

 The flow chart of chap0202 is as follows:

 This form of an if statement executes one of the two statements, not both or neither.

Beginners may make the following two mistakes when writing if statements:

if a < b cout << "a小于b。";           //条件缺少括号
if (c > d) else b = 3;                 //else前缺少语句

Nested if statements

Nesting means: adding one or more tables, images or layers to an existing table, image or layer. Or when two objects have an assembly relationship, one object is embedded in another object.

So nesting if statements means nesting one if statement within another if statement. (Not only one if statement can be nested, as long as there are enough conditions, you can nest as many as you want)

Next, let's look at a nested if statement with a different structure :

//chap0203

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout << "整数值:";
	cin >> n;

	if (n > 0)
		if (n % 2 == 0)
			cout << "这个值为偶数。\n";
		else
			cout << "这个值为奇数。\n";
	else
		cout << "输入了非正值。\n";
}

If the read integer value is positive, display whether it is even or odd; if it is negative, display the corresponding information.

Broken!----Syntax of if statement

Please think about statement 1 and statement 2 in the following code:

if (x == 1)
    if(y == 1)
       语句1;
else
    语句2;

When we encounter such a question, we will have two answers after thinking about it:

plan 1

Statement 1 When x is 1 and y is also 1
Statement 2 when x is not 1

Obviously this is the answer for most beginners, but the reality is always cruel. In such an if statement, the else is in the rule corresponding to the if that is closest to it. That is to say. The rage corresponding to the else in the if statement above is if(x == 1), but if(y == 1). If the first plan does not work, then we can only pin our hopes on the second.

Scenario 2

Statement 1 When x is 1 and y is also 1
Statement 2 when x is 1 and y is not 1

If we write the code like this, maybe there will be no trouble:

if(x == 1)
    if(y == 1)
       语句1;
    else
       语句2;

switch statement

The magic of switch

//chap0204

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout << "请输入整数:";
	cin >> n;

	switch (n) {
	 case 0 : cout << "A";
			  cout << "B";		break;
	 case 2 : cout << "C";
	 case 5 : cout << "D";		break;
	 case 6 :
	 case 7 : cout << "E";		break;
	 default: cout << "F";		break;
	}
	cout << "\n";
}

Label

If the value of condition n is 5, the program flow jumps directly to "case 5:":

 Like case 5: A marker that indicates where the program is going to jump to is called a label .

No space can be typed between 5 and :, but please remember that there must be a space between case and 5.

break statement

When the program flow in chap0204 encounters a break statement, i.e.

break;          //break语句:跳出switch语句

, will end the execution of the switch statement. break means "break" and "jump out" . After the break statement is executed, the program flow will break the switch statement enclosing it and jump out.

So, after jumping out through the break statement, the program will execute the statement following the switch statement.

default tag

Label default: When the job search result of the condition used for the program flow branch does not match any case, the program flow will jump to the default label. Let's take a look at its flow chart:

As can be seen, where there is no break statement, the program flow "slides" to the next statement.

If you change the order in which the labels appear in the switch statement, the result of the program will also change. When using the switch statement, you must consider the label order!

 

Communication between if statement and switch statement

Anyone who has read the above part knows that both the if statement and the switch statement can implement the branch structure of the program flow, and the two are collectively referred to as the selection statement . For a branching structure that can be implemented with an if statement or a switch statement, it is easier to read with a switch statement . Let's take a look at the following figure:

The two pieces of code above are equivalent . In consecutive if statements, the comparison object used for program flow branching does not have to be an expression . For the final judgment statement, one might misread it as if (p == 4) , or suspect that it is a misspelling of if (p == 4) . From this point of view, the switch statement facilitates the overview of the overall situation, and can also reduce the confusion of the reader. So to sum up: instead of using an if statement, it is better to use a switch statement to branch program flow based on an expression!

 

To be continued

I am very grateful to you for reading this. The excitement of "Dou Po CPP" does not stop there. For those who have spare capacity to learn, you can check out the following blogs, all of which have a lot of skills and dry goods ❤ !

The first chapter of "Fighting Break CPP"---- A first glimpse of CPP: https://blog.csdn.net/qq_64263760/article/details/124053651

C language game (1) ---- guessing game:

https://blog.csdn.net/qq_64263760/article/details/122880908
 

If you think this blog is helpful to you who are learning programming, please give Shi Lu. more support and attention ! In the next period of time, Shi Lu. will fight against CPP with my friends. I hope that I can provide you with better blog content next time, and I hope that the next blog will have you !

Guess you like

Origin blog.csdn.net/qq_64263760/article/details/124065720