C++ Review Road Five - Statement

Statement : An expression followed by a semicolon forms a statement.

1. The simplest statement - the empty statement

An empty statement consists of only a semicolon.

; //empty statement

[Note]: When writing code, don't forget to write semicolons, and don't write more semicolons . Otherwise our code may be against our intent

if(i == 0); //If i is equal to zero, do nothing, but it is possible that if i is equal to 0, assign the value of 1 to i
 i = 1; //Writing this way, regardless of whether i is 0, i will be assigned a value of 1

If you want to achieve the above idea, you should write

if(i == 0) //This is to assign a value to i when i is equal to 0. If i is not 0, it will not assign a value of 1 to i
 i = 1;

Through the above two examples, we can know the importance of adding or not adding a semicolon!

The compound statement

A compound statement refers to a sequence of statements and declarations enclosed in curly braces, and compound statements are also known as blocks .

[Note] A block is a scope! , the compound statement does not end with a semicolon.

The scope of the statement

A variable defined in a control structure is only visible inside the corresponding statement, and once the statement ends, the variable goes out of scope.

4. Conditional Statements

C++ provides two conditional execution statements, the if statement and the switch statement

if statement

Determine whether a condition is true, and decide whether to perform the corresponding operation according to the result of the judgment.

There are two forms of if statement

if(condition)
 statement;   

if(condition)
  statement1;
else
  statement2;
When using nested if else statements, pay attention to the matching of if and else, the else matches the if that is closest to it and has not yet been matched

switch statement

form of switch statement

switch (expression)
{
 case label 1: statement1; break;
 case label 2: statement 2; break;
 default : statement 3; break;
}

The switch statement will compare with the label after the case according to the value of the expression. If there is a match, the program will be executed. Otherwise, it will keep looking down. If all the labels are compared, no match is found. , it will enter the default statement.

[Note]: The break after each case is not necessary. If there is no break, when a matching label is found, it will start execution from this statement until the switch statement ends. If we only want to execute one of the statements, we have to add a break ;

Example: without break

int num = 2;

switch (num)
{
case 1: cout << 'a';
case 2: cout << 'b';
case 3: cout << 'c';
default: cout << " no this num" << endl;
	break;
}         

Finally, bc no this num is output. First, switch will find the matching case according to num, find case2, and then divide the number b. Since there is no break, it will continue to execute.

Example 2: With break:

int num = 2;

switch (num)
{
case 1: cout << 'a'; break;
case 2: cout << 'b'; break;
case 3: cout << 'c'; break;
default: cout << "no this num" << endl;
	break;
}

Finally output b, switch finds case2, after execution, because there is a break, it will jump out of the switch, so only one b is output.

Suggestion: When we write code, it is best to add a break after each case.

Five, iterative statement

The iteration statement is what we call the loop. There are three types of iterative statements in C++, the for statement, the while statement, and the do while statement.

while statement

grammatical form

while (condition)
  statement;

The statements inside the loop body are executed as long as the condition is true.

for statement

grammatical form

for(init-statement; condition; expression)
 statement;

init-statement is responsible for initializing a value, which will change with the second loop. condition is a judgment condition. If the condition is satisfied, the statement in the loop body, that is, statement, will be executed, and then expression will modify the value initialized by init_statement.

do while statement

grammatical form

do
  statement;
while(condition);

The do while statement first executes the loop body once, and then checks whether the condition is met.

【Note】: Do not miss the semicolon after while!

Difference between do while and while

do while executes the loop body at least once. Execute first, judge later

while is first judged and then executed.

6. Jump Statement

1. break statement

The break statement is responsible for terminating the while, do while, switch, and for statement closest to it, and starts execution from the first statement after these statements.

2. continue statement

The continue statement is responsible for terminating the current iteration of the loop closest to it and starting the next iteration.

[Note] continue can only appear in while, do while, for statements , or inside statements or blocks nested in such loops .

The difference between break statement and continue statement:

break: End the loop and jump out of the entire loop body , starting execution from the next statement in the loop.

continue: End the current loop and start the next loop without jumping out of the entire loop body.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949094&siteId=291194637