C ++ loop

There is a situation where you need to make a code a few times. In general, statements are executed sequentially.

There is a situation where you need to make a code a few times. In general, statements are executed sequentially. The first statement of the function is executed first, then the second sentence and so on.

The programming language provides us with many control structures and allows you to perform complex parts.

The loop allows to execute a command and a group of commands multiple times, below is the general form:

C ++ supports the following loop. Click on the link to see details.

Loop Description

The while loop in C ++

Repeat one or more groups of commands while the given condition is true. It checks the condition before executing the loop body.

For loop in C ++

Execute a series of commands multiple times and summarize the code that manages loop variables.

The do . while loop in C ++

Same as the While command, except that it checks the condition at the end of the loop body.

Nested loop in C ++

You can use one or more loops in other while, for or do.while loops.

While loop in C / C ++

A while loop command in the C / C ++ Program Language performs a repeat of a target command until the given condition is true.

Syntax

The while loop syntax in C / C ++ Program Language is:

 while ( dieu_kien ) { cac_lenh ; } 

Here, cac_lenh can be a single command or a block of commands. dieu_kien can be any expression, and true is any value other than 0. Repeated iteration while dieu_kien is true.

When the condition becomes false, the control program immediately switches to the command line immediately after the loop.

diagram

Picture 1 of C ++ loop

Here, the main point of the while loop is that it may not run. Because when checking the condition and the result is false, the loop body is ignored and the first command immediately after the loop will be executed.

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 4 ; // vong lap while while ( a < 10 ) { cout << "Gia tri cua a la: " << a << endl ; a ++; } return 0 ; } 

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

Picture 2 of C ++ loop

For loop in C ++

The for loop in C ++ is a repetitive control structure that allows you to write a loop effectively, which needs to be done in a specific time period.

Syntax

The syntax of a for loop in C ++ Program Language is:

 for ( bien ; dieu_kien ; tang_giam ) { cac_lenh ; } 

Below is a description of the control line in a for: loop.

Step bien is done first and only once. This step allows you to declare and initialize any loop control variable. You are not required to place an order here, as long as a semicolon appears.

Next, dieu_kien is estimated. If the condition is true, the loop body is executed. If it is false, the loop body is not executed and the control flow jumps to the next command immediately after the for loop.

After the for loop body executes, the control line jumps to the tang_giam command. This command allows you to update any loop control variable. This command can be left blank, as long as a semicolon appears after the condition.

dieu_kien is now estimated again. If true, the loop executes and the process repeats itself (the loop body, then tang_giam, and then checks the condition again). After the condition becomes false, the for loop ends.

Diagram:

Picture 3 of C ++ loop

For example:

 #include using namespace std ; int main () { // vong lap for for ( int a = 5 ; a < 15 ; a = a + 1 ) { cout << "Gia tri cua a la: " << a << endl ; } return 0 ; } 

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

Picture 4 of C ++ loop

Do . while loop in C ++

Unlike for and while loops, which check loop conditions at the first step of the loop, the do . while loop in C ++ Language checks its condition at the end of the loop.

A do . while loop is similar to a while loop, except at the point of a do . while loop that guarantees to execute the loop at least once.

Syntax

The syntax of a do . while loop in the C ++ Program Language is:

 do { cac_lenh ; } while ( dieu_kien ); 

Notice that the conditional expression appears at the end of the loop, so the instructions in the loop are executed once before the condition is checked.

If the condition is true, the loop control returns, and the commands in the loop are executed again. This process repeats until the condition has become false.

diagram

Picture 5 of C ++ loop

For example:

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 5 ; // Vong lap do.while do { cout << "Gia tri cua a la: " << a << endl ; a = a + 1 ; } while ( a < 15 ); return 0 ; } 

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

Picture 6 of C ++ loop

Nested loop in C ++

The C ++ program language allows you to use a loop inside a loop. Here are some examples that illustrate this concept.

Syntax

The syntax for nesting for loop in C ++ is as follows:

 for ( bien ; dieu_kien ; tang_giam ) { for ( bien ; dieu_kien ; tang_giam ) { cac_lenh ; } cac_lenh ; // ban co the dat nhieu lenh tai day. } 

The syntax to nest while loop in C ++ is as follows:

 while ( dieu_kien ) { while ( dieu_kien ) { cac_lenh ; } cac_lenh ; // ban co the dat nhieu lenh tai day. } 

The syntax to nest do . while loop in C ++ is as follows:

 do { cac_lenh ; // ban co the dat nhieu lenh tai day. do { cac_lenh ; } while ( dieu_kien ); } while ( dieu_kien ); 

For example

The following program uses a for loop to find primes from 2 to 50:

 #include using namespace std ; int main () { int i , j ; for ( i = 2 ; i < 50 ; i ++) { for ( j = 2 ; j <= ( i / j ); j ++) if (!( i % j )) break ; // neu tim thay he so, thi khong la so nguyen to if ( j > ( i / j )) cout << i << " la so nguyen ton" ; } return 0 ; } 

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

Picture 7 of C ++ loop

C ++ control commands

Loop control commands change the execution of commands from its usual range. When the execution of the command leaves a range, all automatic objects that are created within that scope are destroyed.

C ++ supports the following loop control commands. Click on the following links for more details.

Control command Description

Break command in C ++

End the loop or switch command and switch to executing the loop or switch command immediately after it.

The continue command in C ++

When this command is encountered, the program will ignore the statements below it (in the same loop statement) to execute the new loop.

Goto command in C ++

Go to the assigned command. However, it is advised not to use the goto command in your program.

Break command in C ++

The break command in C ++ has two uses:

When the break command is used in the loop, the loop immediately ends and controls the start of the next instruction after the loop.

It can be used in the switch command (discussed in the next chapter).

If you are using nested loops (for example, a loop inside another loop), the break statement will stop executing a certain command in a loop and start executing the next command of the following code. That code block.

Syntax

The syntax of break command in C ++ is as follows:

 break ; 

diagram

Picture 8 of C ++ loop

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 10 ; // Vong lap do.while do { cout << "Gia tri cua a la: " << a << endl ; a = a + 1 ; if ( a > 15 ) { // Ket thuc vong lap break ; } } while ( a < 20 ); return 0 ; } 

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

Picture 9 of C ++ loop

The continue command in C ++

The continue statement in C ++ works a bit like the break statement. Instead of forcing it to end, it forces the next loop to take place, ignoring any code in between.

With the for loop, the continue statement causes the step to check the condition and the tang_giam part of the loop to execute. With while and do . while, the continue command makes the program control move to the conditional checks.

Syntax

The syntax of the continue statement in C ++ is as follows:

 continue ; 

diagram

Picture 10 of C ++ loop

For example

 #include using namespace std ; int main () { // Khai bao bien cuc bo: int a = 10 ; // vong lap do.while do { if ( a == 15 ) { // nhay qua buoc lap. a = a + 1 ; continue ; } cout << "Gia tri cua a la: " << a << endl ; a = a + 1 ; } while ( a < 20 ); return 0 ; } 

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

Picture 11 of C ++ loop

Goto command in C ++

The goto command in the C ++ Program Language provides an unconditional jump from the goto to the command to be labeled in the same function.

Note: Using the goto command makes it difficult for any program language because it makes it difficult to track the flow of a program, making it difficult to understand and difficult to edit. Any program that uses a goto command can be rewritten so that there is no need for this goto command.

Syntax

The syntax of goto command in C ++ is as follows:

 goto label ; . . label : lenh ; 

Here, the label can be any text except the keywords in C ++, and it can be set anywhere in the C ++ program, above or below this goto command.

diagram

Picture 12 of C ++ loop

For example

 #include using namespace std ; int main () { // Declare the user: int a = 10 ; // Turn the lap do . while VONGLAP : because // is a VONGLAP, and it is caused by { if ( a == 15 ) { // click through the lap. a = a + 1 ; goto VONGLAP ; } cout << "Family of a la:" << a << endl ; a = a + 1 ; } while ( a < 20 ); return 0 ; } 

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

Picture 13 of C ++ loop

A good use of the goto command is to get rid of a deep loop. For example, consider the following code:

 for (.) { for (.) { while (.) { if (.) goto stop ; . . . } } } stop : cout << "Xuat hien loi trong chuong trinh.n" ; 

With no goto, the program will perform additional tests. Meanwhile, a break statement should not be used here, because it will only cause the program to exit the innermost loop.

Infinite loop in C ++

A loop is an infinite loop when a condition is never false. A for loop is often used for this purpose. When you leave three conditional expressions in a for loop, you will create an infinite loop.

 #include using namespace std ; int main () { for ( ; ; ) { printf ( "Vong lap nay se chay mai mai.n" ); } return 0 ; } 

When the conditional expression is absent, it is assumed to be true. You can have an incremental and decreasing expression, but C ++ programmers often use for (;;) to represent an infinite loop.

Note : You can stop (end) an infinite loop by pressing Ctrl + C.

According to Tutorialspoint

Previous article: Operator in C ++

Next lesson: Control flow in C ++

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile