C语言学习笔记--break、continue、return和goto

1、break:
    break 语句,它不仅可以跳出“循环体”,还可以跳出switch语句。同时,break 也只能用于这两种情况。break 语句不能用于循环语句和 switch 语句之外的任何其他语句中。
    break是用于永久终止循环。即不执行本次循环中break后面的语句,直接跳出循环。
    不管是for循环,还是while循环,或者是do…while循环,都可以用break 跳出来,但是break只能跳出一层循环。当有多层循环嵌套的时候,break只能跳出“包裹”它的最里面的那一层循环,无法一次跳出所有循环。    
    同样,在多层switch嵌套的程序中,break也只能跳出其所在的距离它最近的switch。
2、continue:
    continue是用于终止本次循环。即本次循环中continue后面的代码不执行,进行下一次循环的入口判断。
3、return:
    return关键字并不是专门用于跳出循环的,return的功能是结束一个函数。 一旦在循环体内执行到一个return语句,return语句将会结束该函数,循环自然也随之结束。与continue和break不同的是,return直接结束整个函数,不管这个return处于多少层循环之内。     
4、goto:
    goto 语句是一种无条件流程跳转语句,goto可以跳出多重循环,标号只是标号,程序到标号位置正常执行。标号仅仅起到一个标识作用,程序顺序执行时,标号仿佛不存在。并不是只有调用了goto标号,然后程序跳到标号处,从而触发标号下边的代码开始运行。

    continue和break的区别
        continue语句和break语句的区别是,continue 语句只结束本次循环,而不是终止整个循环。break 语句则是结束整个循环过程,不再判断执行循环的条件是否成立。而且,continue 只能在循环语句中使用,即只能在 for、while 和 do…while 中使用,除此之外 continue 不能在任何语句中使用。
        continue不能在switch中使用,除非switch 在循环体中。此时 continue 表示的也是结束循环体的本次循环,跟 switch 也没有任何关系。
    goto语句建议
        1) 使用goto语句只能goto到同一函数内,而不能从一个函数里goto到另外一个函数里。
        2) 使用goto语句在同一函数内进行goto时,goto的起点应是函数内一段小功能的结束处,goto的目的label处应是函数内另外一段小功能的开始处。
        3) 不能从一段复杂的执行状态中的位置goto到另外一个位置,比如,从多重嵌套的循环判断中跳出去就是不允许的。
        4)应该避免向两个方向跳转。这样最容易导致"面条代码"。
程序测试部分:
    //1、reak测试程序:
    #include<stdio.h>
    /**
     *********************************************************************************
     * @file             main.c
     * @auther           BA-li
     * @version          1.1.1
     * @data             2018-12-26 16:04:41
     * @brief            break和continue功能测试函数
     * @param            none
     * @retval           none
     * @attention
     *********************************************************************************
     */
    int
    main(void)
    {
        printf("This is a break/continue/goto/return test!!!\r\n");

        int i = 0;

        while (i < 10)
        {
            i = i + 1;

            if (i == 5)
                break;

            printf("%d  ",i);

        }

        return 0;
    }
    /** 程序输出结果:
     *****************************************
     * This is a break/continue/goto/return test!!!
     * 1  2  3  4
     *****************************************
     */
     
    //2、continue测试程序:
    #include<stdio.h>
    /**
     *********************************************************************************
     * @file             main.c
     * @auther           BA-li
     * @version          1.1.1
     * @data             2018-12-26 16:04:41
     * @brief            break和continue功能测试函数
     * @param            none
     * @retval           none
     * @attention
     *********************************************************************************
     */
    int
    main(void)
    {
        printf("This is a break/continue/goto/return test!!!\r\n");

        int i = 0;

        while (i < 10)
        {
            i = i + 1;

            if (i == 5)
                continue;

            printf("%d  ",i);

        }

        return 0;
    }
    /** 程序输出结果:
     *****************************************
     * This is a break/continue/goto/return test!!!
     * 1  2  3  4  6  7  8  9  10
     *****************************************
     */
     
    //3、return测试程序:
    #include<stdio.h>
    /**
     *********************************************************************************
     * @file             main.c
     * @auther           BA-li
     * @version          1.1.1
     * @data             2018-12-26 16:04:41
     * @brief            return功能测试函数
     * @param            none
     * @retval           none
     * @attention
     *********************************************************************************
     */
    int
    main(void)
    {
        printf("This is a break/continue/goto/return test!!!\r\n");

        int i = 0;

        while (i < 10)
        {
            i = i + 1;

            if (i == 5)
                return 0;

            printf("%d  ",i);

        }

        return 0;
    }
    /** 程序输出结果:
     *****************************************
     * This is a break/continue/goto/return test!!!
     * 1  2  3  4
     *****************************************
     */
    
    //4、goto测试程序:
    #include<stdio.h>
    /**
     *********************************************************************************
     * @file             main.c
     * @auther           BA-li
     * @version          1.1.1
     * @data             2018-12-26 16:04:41
     * @brief            goto功能测试函数
     * @param            none
     * @retval           none
     * @attention
     *********************************************************************************
     */
    int
    main(int argc, char **argv)
    {
        int i = 1;

        while(1)
        {
            printf("while(1) \r\n");
            while(i++)
            {
                printf("while(i++) i = %d\n",i);
                if(i > 3)
                {
                    goto program_exti;
                }
            }
        }
    program_exti:
        printf("程序结束\n");
        return 0;
    }
    /**程序输出结果
     ************************************
     *while(1)
     *while(i++) i = 2
     *while(i++) i = 3
     *while(i++) i = 4
     *程序结束
     ************************************
     */
 

#include <stdio.h>
int
main(int argv, char ** argc)
{
    unsigned char a = 2;

    if (a == 1) {
        goto test1;
    } else if (a == 2) {
        goto test2;
    } else if (a == 3){
        goto test3;
    } else {
        goto test4;
    }

    test1:
        printf("test1\r\n");
    test2:
        printf("test2\r\n");
    test3:
        printf("test3\r\n");
    test4:
        printf("test4\r\n");

    printf("random test\r\n");

}
/** output result
*******************************
test2
test3
test4
random test
*******************************
 */

猜你喜欢

转载自blog.csdn.net/tyustli/article/details/85264973