This is the sixth day of my participation in the August Text Challenge.More challenges in August

directory

  • Use break in the for loop
  • Use continue in the for loop
  • Guess you like it

C/C++ learning Directory >> C language basics

The previous article covered the use of the for loop in detail, but this article also introduces two additional keywords: **break ** and continue, which are usually used with loops;

oneforUse in circulationbreak

A for loop can loop 100 times or even 10000 times, and it will always end. If the expression 2 is always true, it will never end. Such as:

for(;;)
Copy the code

The for loop is a perpetual loop that never ends until the seas run dry and the rocks crumble… If we want to solve this awkward problem, we can do it by using the keyword break; Using break in a loop means to terminate the loop immediately.

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: apes said programming / / @ Blog (personal Blog address) : www.codersrc.com // @file :C language break and continue // @time :2021/05/31 08:00 //@Motto: No accumulation of small steps to a thousand miles, no accumulation of small streams into rivers and seas, the wonderful program life needs unremitting accumulation! /************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < 100; i++) { if (i > 10) break; printf("i = %d\n", i); } system("pause"); return 0; } /* Output: I = 0 I = 1 I = 2 I = 3 I = 4 I = 5 I = 6 I = 7 I = 8 I = 9 I = 10 Press any key to continue.. */Copy the code

If the value of I is greater than 10, break the loop and wait for the program to exit…

twoforUse in circulationcontinue

Using continue in a loop means that the code after the continue will not be executed and the next loop will continue. This can be used as a filter in a loop, for example: 0 to 100, 0, 10 and 90, 99.

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: apes said programming / / @ Blog (personal Blog address) : www.codersrc.com // @file :C language break and continue // @time :2021/05/31 08:00 //@Motto: No accumulation of small steps to a thousand miles, no accumulation of small streams into rivers and seas, the wonderful program life needs unremitting accumulation! /************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "windows.h" #include <stdarg.h> int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < 100; i++) { if (i > 10 && i<90) continue;; printf("i = %d\n", i); } system("pause"); return 0; } /* Output:  i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 i = 90 i = 91 i = 92 i = 93 i = 94 i = 95 i = 96 i = 97 I = 98 I = 99 Press any key to continue.. */Copy the code

If condition 2 is true, the loop continues until the end of the for loop, waiting for the program to exit. If condition 2 is true, the loop continues until the program exits.

3.Guess you like

  1. Install Visual Studio
  2. Install the Visual Studio plug-in, Visual Assist
  3. Visual Studio 2008 uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. Set the Visual Studio font/background/line number
  6. C Hello World
  7. C code comments
  8. C data type/variable type
  9. C language variable declaration and definition
  10. C format controller/placeholder
  11. C printf function
  12. C conditional judgment if/else
  13. C language logic operator
  14. C language ternary operator
  15. C language comma expression
  16. C (++ I/I ++)
  17. C language for loop
  18. C language break and continue

C language break and continue

This article is published by the blog – Ape Say Programming Ape Say programming!