[aftertaste C] cycle

;

Omitted expression in for statement:

    //[1]
    for (int i=0; i<10; i++) {
        printf("%d",i);
    }

    //[2]
    int j = 10;
    for (; j>0; j--) {
        printf("%d",j);
    }

    //[3]
    //If omittedforFor the third expression in the statement, the body of the loop needs to ensure that the value of the second expression eventually becomes false.

    for (int k = 10 ; k > 0 ;) {
        printf("%d",k--);
    }

    //[4]
    //If omittedforWhen you loop through the first and third expressions, it and white There is no difference.
    int m = 10;
    for (; m > 0 ;) {
        printf("%d",m--);
    }

    /*etc.The price is as follows:*/
    int n = 10;
    while (n > 0) {
        printf("%d",n--);
    }

continue Statement

    int n = 0;
    int sum = 0;
    int i;
    while (n < 10) {
        printf("Input:");
        scanf("%d",&i);
        if (i == 0)
            continue;
        sum += i;
        n++;
    }
    NSLog(@"%d",sum);

Equivalent to:

    int n = 0;
    int sum = 0;
    int i;
    while (n < 10) {
        printf("Input:");
        scanf("%d",&i);
        if (i != 0){
            sum += i;
            n++;
        }
    }
    NSLog(@"%d",sum);

go to statement

    int d = 0;
    int n = 2;
        for (d = 2; d > n; d++) {
        if (n % d == 0) {
            goto choose;
        }
    }

    choose:
    if (d < n) {
        printf("%d is divisible by %d \n",n ,d );
    }else{
        printf("%d is prime\n",n);
    }

//2 is prime

In most cases, it's enough to deal with most cases of goto statements. Because the goto statement is a demon that is hard to control.

Consider exiting from the body of the loop that contains the switch statement. As you saw earlier, the break statement does not have the expected effect; it can jump out of the switch statement, but not out of the loop. The goto statement solves this problem.

    int i = 5;
    while (i > 0) {

        NSLog(@"%d",i);
        switch (i) {
            case 3:
                NSLog(@"this is 3");
                break;
            default:
                break;
        }
        i--;
    }

/*
2017-12-15 17:56:28.179698+0800 qweqw[49047:839251] 5
2017-12-15 17:56:28.179961+0800 qweqw[49047:839251] 4
2017-12-15 17:56:28.179984+0800 qweqw[49047:839251] 3
2017-12-15 17:56:28.180038+0800 qweqw[49047:839251] this is 3
2017-12-15 17:56:28.180055+0800 qweqw[49047:839251] 2
2017-12-15 17:56:28.180084+0800 qweqw[49047:839251] 1
*/

If we use the goto statement;

    int i = 5;
    while (i > 0) {

        NSLog(@"%d",i);
        switch (i) {
            case 3:
                NSLog(@"this is 3");
                goto hello;
                break;
            default:
                break;
        }
        i--;
    }

hello:
    NSLog(@"print hello");

/*
2017-12-15 17:57:15.669422+0800 qweqw[49071:840022] 5
2017-12-15 17:57:15.669680+0800 qweqw[49071:840022] 4
2017-12-15 17:57:15.669689+0800 qweqw[49071:840022] 3
2017-12-15 17:57:15.669697+0800 qweqw[49071:840022] this is 3
2017-12-15 17:57:15.669708+0800 qweqw[49071:840022] print hello
*/

================================================

Posted by Bit343 on Tue, 19 May 2020 09:08:41 -0700