C and C ++ from scratch series (four)

After the conditional, we learn about recycling. Enter an array allows you to assign each element in the array. Reading a file, each line of the output file. Etc. These will be used in circulation.

We in C and C ++, a native of the main loop for loop and while loops. Wherein the while loop is divided into while () {} and do {} while () both (referred to as a lot until the circulating type and textbooks cycle).

This is our three sample usage.

 1 int main()
 2 {
 3         int a[10] ={0};
 4         for (int x = 0; x < 10; x++)
 5         {
 6                 a[x]=2*x;
 7         }
 8         for (int x = 0; x < 10; x++)
 9         {
10                 printf("The %d element is %d\n", x+1, a[x]);
11         }
12         return 0;
13 }

Some compiler for the extension parameters are certain requirements in the 4.8.5 version of gcc I use.

1. If the file extension is .c, when I use the gcc compiler, suggesting that I only use for loop in C99 mode.

2. The same code, when compiled using g ++ compiler success

3. If the extension is .cc, using gcc and g ++ compilers are successful.

4. VS2015 this problem does not arise.

5. VC6 will be determined for the second loop is repeated x defined.

 

There are three expressions for loop, with an intermediate; separated. The first is the initial expression cycle start, the loop end condition is the second, the third statement is executed after each cycle, to update the variable, gradually approaching the end of such cycling conditions.

Therefore, in the cycle following the program sequence for the above example:

1. Define a variable x, and assign an initial value 0,

2. Analyzing x satisfies the second expression x is less than 10

3. If not (x <10), exit the loop 

4. If so, the for loop statement in vivo

The for loop in the third expression x ++

6. Repeat steps 2-5.

Loop may be used to break the loop ends. (For and while both are true).

So some circumstances the second expression program will appear empty. The above example, if the second expression is empty, so to write

 1 int main()
 2 {
 3         int a[10] ={0};
 4         for (int x = 0; ; x++)
 5         {
 6                 if (x >= 10)
 7                         break;
 8                 a[x]=2*x;
 9         }
10         for (int x = 0; ; x++)
11         {
12                 if (x >= 10)
13                         break;
14                 printf("The %d element is %d\n", x+1, a[x]);
15         }
16         return 0;
17 }

And some examples will be the third expression is also empty, then we write it so

 1 int main()
 2 {
 3         int a[10] ={0};
 4         for (int x = 0; ; )
 5         {
 6                 if (x >= 10)
 7                         break;
 8                 a[x]=2*x;
 9                 x++;
10         }
11         for (int x = 0; ; )
12         {
13                 if (x >= 10)
14                         break;
15                 printf("The %d element is %d\n", x+1, a[x]);
16                 x++;
17         }
18         return 0;
19 }

Continue to transform the for loop, because we know that in a for loop, the first expression only needs to be performed only when entering the loop, that we may be performed before the for loop. Ensure there is no duplication in the case defined under the variable x, we changed for cycles

 1 int main()
 2 {
 3         int a[10] ={0};
 4         int x = 0;
 5         for (; ; )
 6         {
 7                 if (x >= 10)
 8                         break;
 9                 a[x]=2*x;
10                 x++;
11         }
12         x=0;
13         for (; ; )
14         {
15                 if (x >= 10)
16                         break;
17                 printf("The %d element is %d\n", x+1, a[x]);
18                 x++;
19         }
20         return 0;
21 }

Then the for loop expression is empty, we can easily put a for loop instead of the while loop.

Read this:

 1 int main()
 2 {
 3         int a[10] ={0};
 4         int x = 0;
 5         while(1)
 6         {
 7                 if (x >= 10)
 8                         break;
 9                 a[x]=2*x;
10                 x++;
11         }
12         x=0;
13         while (1 )
14         {
15                 if (x >= 10)
16                         break;
17                 printf("The %d element is %d\n", x+1, a[x]);
18                 x++;
19         }
20         return 0;
21 }

In this way we will be able to change the for loop while loop, or while the specific use for the time to write the code? All OK, no better than what people say. Just need to use a for loop C99 mode, if you need to write the code to be run in the old environment, you can only use a while loop.

while we use while (1), C, a 0 is false, 0 is a non-true.

Next we repeat the while loop.

Guess you like

Origin www.cnblogs.com/danielhu/p/12079377.html