Latest web development tutorials

C ++ loop

Sometimes, you may need to perform several times the same piece of code. Under normal circumstances, the statement is the order of execution: the function of the first statement executed first, followed by a second statement, and so on.

Programming languages ​​that allow more complex execution paths of various control structures.

Loops allow us to repeatedly execute a statement or group of statements, the following is the general form in most programming languages ​​looping statement:

Loop structure

Type of cycle

C ++ programming language provides the following cycle types. Click on the link to view the details of each type.

while 循环 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for 循环 多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环 除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环 您可以在 while、for 或 do..while 循环内使用一个或多个循环。

Loop control statements

Loop control statements to change the normal sequence of execution. When performing a range of leave, all created in the target range will be automatically destroyed.

C ++ provides the following control statements. Click on the link to view the details of each statement.

break 语句 终止loopswitch语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
continue 语句 引起循环跳过主体的剩余部分,立即重新开始测试条件。
goto 语句 将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。

Infinite loop

If the condition is never false, the loop becomes an infinite loop.for circulation in the traditional sense it can be used to implement an infinite loop.Since the three expressions constitute any one cycle is not required, you can be certain conditional expression blank to form an infinite loop.

#include <iostream>
using namespace std;
int main ()

   for( ; ; )
      printf("This loop will run forever.\n");

   return 0;

When the conditional expression does not exist, it is assumed to be true. You can also set an initial value and the increment expression, but under normal circumstances, C ++ programmers prefer to use for (;;) structure to represent an infinite loop.

Note: You can press Ctrl + C to terminate an infinite loop.