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;
onefor
Use 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…
twofor
Use 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
- Install Visual Studio
- Install the Visual Studio plug-in, Visual Assist
- Visual Studio 2008 uninstall
- Visual Studio 2003/2015 Uninstall
- Set the Visual Studio font/background/line number
- C Hello World
- C code comments
- C data type/variable type
- C language variable declaration and definition
- C format controller/placeholder
- C printf function
- C conditional judgment if/else
- C language logic operator
- C language ternary operator
- C language comma expression
- C (++ I/I ++)
- C language for loop
- C language break and continue
C language break and continue
This article is published by the blog – Ape Say Programming Ape Say programming!