首页 > 代码库 > 8 break 和 Continue 之间的区别

8 break 和 Continue 之间的区别

break: 直接跳出循环

continue:中断本次循环,继续进行下一次循环

        static void breakvsContinue()
        {
            for (int i = 0; i < 10; i++)
            {
                if (i == 0) break;
                DoSomeThingWith(i);
            }

            the break will cause the loop to exit on the first iteration - DoSomeThingWith will never be executed.
            for (int i = 0; i < 10; i++)
            {
                if (i == 0) continue;
                DoSomeThingWith(i);
            }
        }