C and C ++ from scratch series (six)

Speaking in front of C and C ++ for use and ways to achieve while loop in C and C ++, there is a way to achieve cycle, that is, goto. Although we do not recommend the use of goto.

This is while in the previous cycle program.

 1 int main()
 2 {
 3         int a[10] ={0};
 4         int x = 0;
 5         while( true )
 6         {
 7                 if (x >= 10)
 8                         break;
 9                 a[x]=2*x;
10                 x++;
11         }
12         x=0;
13         while ( true )
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 }

We changed to goto way to achieve:

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

Goto use is not recommended in some new language, goto is defined as a keyword and is not allowed to use.

But goto functional break and continue can not be achieved. Such as a plurality of nested loops, when the innermost loop when certain conditions are satisfied, completely out of the outermost loop needs, using the goto statement, the program can be simplified, reducing the use of variables.

 1 int main()
 2 {
 3         int x = 0;
 4         for (int i = 0; i < 10; i++)
 5         {
 6                 for (int j = 0; j < 10; j++)
 7                 {
 8                         printf("Input x=");
 9                         scanf("%d", &x);
10                         if (x % 2 == 0)
11                         {
12                                 printf("Input an odd number.\n");
13                                 break;
14                         }
15                         else
16                         {
17                                 printf("Input an even number.\n");
18                                 goto endflag;
19                         }
20                 }
21                 printf("Current i=%d.\n", i+1);
22         }
23 endflag:
24         printf("Finishe.\n");
25 }

9 waits for user input line numbers.

Lines 10-14, it is judged if the input number is an even number, out of the current cycle, the program execution line 21.

Lines 15 to 19, it is determined if the input number is an odd number, directly out of two cycles, 24 lines of program execution.

If you do not use the goto statement, we need to use other variables.

int main ()
{
        int x = 0;
        for (int i = 0; i < 10; i++)
        {
                bool ret = false;
                for (int j = 0; j < 10; j++)
                {
                        printf("Input x=");
                        scanf("%d", &x);
                        if (x % 2 == 0)
                        {
                                printf("Input an odd number.\n");
                                break;
                        }
                        else
                        {
                                printf("Input an even number.\n");
                                ret = true;
                                break;
                        }
                }
                if (ret)
                        break;
                printf("Current i=%d.\n", i+1);
        }
        printf("Finishe.\n");
}

However, goto can cause difficulties in the maintenance of the program, more use of a variable in the machines now do not have much impact.

Guess you like

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