Control flow in C ++

Flow control structures require the programmer to determine one or more conditions to be evaluated and checked by the program, along with the commands to be executed if the condition is determined to be correct, or other commands are done if the specified condition is wrong.

Flow control structures require the programmer to determine one or more conditions to be evaluated and checked by the program, along with the commands to be executed if the condition is determined to be correct, or other commands are done if the specified condition is wrong.

Below is a common pattern of a common flow control structure in a programming language.

Control flow in C ++ Picture 1Control flow in C ++ Picture 1

The following lists the flow control commands provided by C ++. Click on the link to see details.

Command Description If statement in C ++ An if statement consists of a logical expression followed by one or more other commands. If . else statement in C ++ An if statement can be followed by an else statement (optional: yes or no), which can be executed when the logical expression has a value of false. The switch command in C ++ A switch command allows checking the condition of a variable before executing commands. Nesting if statements in C ++ You can use the if or else if statement inside another if or else if statement. Nesting switch commands in C ++ You can use a switch command inside another switch command.

If statement in C ++

An if statement in C ++ Language contains a boolean expression followed by one or more commands.

Syntax

The following is the syntax of an if statement in C ++:

 if ( bieu_thuc_boolean ) { // cac lenh se duoc thuc thi neu bieu thuc boolean la true } 

If the boolean expression is evaluated as true, then the code block inside the if statement will be executed. If the boolean expression is evaluated to be false, then the command immediately after the if statement will be executed.

diagram

Control flow in C ++ Picture 2Control flow in C ++ Picture 2

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 10 ; // kiem tra dieu kien cua bieu thuc boolean if ( a < 20 ) { // Neu dieu kien la true thi in dong sau cout << "a la nho hon 20." << endl ; } cout << "Gia tri cua a la: " << a << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Control flow in C ++ Picture 3Control flow in C ++ Picture 3

If . else command in C ++

The if statement in C ++ can be followed by an optional else command, which executes when the boolean expression is false.

Syntax

The following is the syntax of an if . else statement in C ++:

 if ( bieu_thuc_boolean ) { // cac lenh se duoc thuc thi neu bieu thuc boolean la true } else { // cac lenh se duoc thuc thi neu bieu thuc boolean la false } 

If the boolean expression is evaluated to be true, then the if block will be executed, otherwise the else block will be executed.

diagram

Control flow in C ++ Picture 4Control flow in C ++ Picture 4

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 100 ; // kiem tra dieu kien cua bieu thuc boolean if ( a < 20 ) { // Neu dieu kien la true thi in dong sau cout << "a la nho hon 20." << endl ; } else { // Neu dieu kien la false thi in dong sau cout << "a khong nho hon 20." << endl ; } cout << "Gia tri cua a la: " << a << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Control flow in C ++ Picture 5Control flow in C ++ Picture 5

If . else if . else command in C ++

An if statement can be followed by an else statement if . else is optional, which is useful for checking various conditions.

When using the if, else if, else statement, there are some key points to keep in mind:

An if statement has 0 or an else command and they must be followed by any else if statement.

An if statement has 0 to many else if statements and they must be before the else statement.

Once the else if command executes, it will not check any else if or else else command.

Syntax

Syntax of an if . else if . else command in C ++ Language is as follows:

 if ( bieu_thuc_boolean 1 ) { // thuc thi khi bieu thuc boolean 1 la true } else if ( bieu_thuc_boolean 2 ) { // thuc thi khi bieu thuc boolean 2 la true } else if ( bieu_thuc_boolean 3 ) { // thuc thi khi bieu thuc boolean 3 la true } else { // thuc thi khi tat ca cac dieu kien tren khong la true. } 

For example:

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 100 ; // kiem tra dieu kien cua bieu thuc boolean if ( a == 10 ) { // Neu dieu kien la true thi in dong sau cout << "Gia tri cua a la 10" << endl ; } else if ( a == 20 ) { // neu dieu kien else if la true cout << "Gia tri cua a la 20" << endl ; } else if ( a == 30 ) { // eu dieu kien else if la true cout << "Gia tri cua a la 30" << endl ; } else { // neu cac dieu kien tren khong la true thi in dong sau cout << "Gia tri cua a khong ket noi voi cac dieu kien tren" << endl ; } cout << "Gia tri chinh xac cua a la: " << a << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Control flow in C ++ Picture 6Control flow in C ++ Picture 6

Switch case command in C / C ++

[switch / case in C / C ++] A switch command in C / C ++ for a variable is checked equally in the list of values. Each value is called a case - the case and the variable passed is checked for each switch case.

Syntax

The syntax of the switch command in C / C ++ Language is as follows:

 switch ( bieu_thuc ){ case bieu_thuc_hang : statement ( s ); break ; //optional case bieu_thuc_hang : statement ( s ); break ; //optional // you can have any number of case statements. default : //Optional statement ( s ); } 

The following rules apply to a switch command:

The expression that is used in a switch command must be of type integer or enumeration, or one of the class types in which the class has a function that converts a single to an integer or enumeration type.

You can have any number of case commands in a switch. Each case is followed by the value to be compared and a colon.

bieu_thuc_hang, is a constant expression, for a case to have the same data type as the variable in the switch, and it must be constant.

When the variable passed is balanced with a case, the following case statement will execute until a break statement is encountered.

When the break command is encountered, the switch ends, and the control line jumps to the next command line of the switch.

Not every case should contain a break statement. If no break statement appears, the control line will not reach the next case until a break statement is encountered.

A switch command may have an optional default case, which must appear at the end of the switch. This default case can be used to perform a task when there is no true case. In this default case, there is no break command required.

diagram

Control flow in C ++ Picture 7Control flow in C ++ Picture 7

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: char hocluc = 'D' ; switch ( hocluc ) { case 'A' : cout << "Gioi!" << endl ; break ; case 'B' : case 'C' : cout << "Kha" << endl ; break ; case 'D' : cout << "Trung binh" << endl ; break ; case 'F' : cout << "Phai hoc lai!!" << endl ; break ; default : cout << "Gia tri khong hop le" << endl ; } cout << "Hoc luc cua ban la " << hocluc << endl ; return 0 ; } 

Running the above C / C ++ program will produce the following results:

Control flow in C ++ Picture 8Control flow in C ++ Picture 8

Nesting if statements in C ++

It is valid to nest if-else statements in C ++, meaning you can use an if or else statement inside another if or else statement.

Syntax

Syntax to nest if statements in C ++ is as follows:

 if ( bieu_thuc_boolean 1 ) { // Thuc thi khi bieu thuc boolean 1 la true if ( bieu_thuc_boolean 2 ) { // Thuc thi khi bieu thuc boolean 2 la true } } 

You can nested else if . else in the same way as if you had inserted the if statement.

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; // kiem tra dieu kien cua bieu thuc boolean if ( a == 100 ) { // neu dieu kien la true thi kiem tra tiep dieu kien sau if ( b == 200 ) { // neu dieu kien la true thi in dong sau cout << "Gia tri cua a la 100 va b la 200" << endl ; } } cout << "Gia tri chinh xac cua a la: " << a << endl ; cout << "Gia tri chinh xac cua b la: " << b << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Control flow in C ++ Picture 9Control flow in C ++ Picture 9

Insert switch commands in C ++

It is possible to have a switch command as part of the command sequence in a switch command on the outer ring. Even if the case constant inside and outside the switch command contains normal values, no conflict will occur here.

C ++ defines at least 256 levels of nesting for the switch command that is allowed.

Syntax

The syntax to nest switch commands in C ++ is as follows:

 switch ( ch1 ) { case 'A' : cout << "A nay la mot phan cua lenh switch ben ngoai" ; switch ( ch2 ) { case 'A' : cout << "A nay la mot phan cua lenh switch ben trong" ; break ; case 'B' : // . } break ; case 'B' : // . } 

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; switch ( a ) { case 100 : cout << "Day la mot phan cua lenh switch ben ngoai" << endl ; switch ( b ) { case 200 : cout << "Day la mot phan cua lenh switch ben trong" << endl ; } } cout << "Gia tri chinh xac cua a la: " << a << endl ; cout << "Gia tri chinh xac cua b la: " << b << endl ; return 0 ; } 

Running the above C ++ program will produce the following results:

Control flow in C ++ Picture 10Control flow in C ++ Picture 10

Conditional operator? : in C ++

We discussed conditional operators? : in the previous chapter that can be used to replace the if . else statement . It has the following general pattern:

 Exp1 ? Exp2 : Exp3 ; 

In which Exp1, Exp2 and Exp3 are expressions. Notice the use and setting of the colon.

The value of the expression Exp1 before the sign? with true value, Exp2 is executed, and its value is the value of the expression. If Exp1 is false , Exp3 is executed and its value is the value of the expression.

According to Tutorialspoint

Previous article: Loop in C ++

Next lesson: Function in C / C ++

5 ★ | 1 Vote