C + + Basics: C + + loop

Time:2021-9-12

Sometimes, you may need to execute the same piece of code multiple times. Generally, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on.

Programming languages provide a variety of control structures that allow more complex execution paths.

Circular statements allow us to execute a statement or statement group multiple times. The following is the general form of circular statements in most programming languages:

 

 

C/C++Learning skirt【seven one two   two eight four   seven zero five  】, whether you areXiaobai is still an advanced person, whether you want to change your career or join it, you can learn about it, make progress together and learn together! There are development tools in the skirt, and many dry goods and technical data are shared!

 

Cycle type

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

 

 

Loop control statement

Loop control statements change the normal sequence of execution. When execution leaves a scope, all automatic objects created in the scope are destroyed.

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

 

Infinite loop

If the condition is never false, the loop becomes an infinite loop.for  Loop can be used to realize infinite loop in the traditional sense. Since none of the three expressions that make up the loop is required, you can leave some conditional expressions blank to form an infinite loop.

#include 
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 an incremental expression, but generally, C + + programmers prefer to use the for (;) structure to represent an infinite loop.

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