Break and Continue Statements in C
Learn how to use break and continue statements in C to control loop execution. Includes clear examples and explanations for Debian 12 and Vim users.
Break and Continue Statements in C Programming
The break and continue statements help control loops in C. They let you alter the flow of execution inside loops for better logic and control.
Break Statement
The break statement immediately exits the loop when a certain condition is met.
Continue Statement
The continue statement skips the rest of the current loop iteration and moves to the next iteration.
Example Program
This program demonstrates both break and continue in a for loop:
#include
int main()
for (int i = 1; i <= 10; i++)
if (i == 5)
break;
if (i % 2 == 0)
continue;
printf(\\"%d\\\
\\", i);
return 0;
Compiling and Running
Compile and run the program on Debian 12:
gcc break_continue.c -o break_continue
./break_continue
Expected Output
1
3
Explanation
- Loop runs from 1 to 10.
- If
iequals 5,breakexits the loop immediately. - If
iis even,continueskips printing and moves to the next iteration. - Only odd numbers less than 5 are printed: 1 and 3.
Conclusion
Using break and continue helps you control loops efficiently and write clearer code.
Subscribe to Our YouTube for More
https://blog.arashtad.com/updates/break-and-continue-statements-in-c/?feed_id=12947&_unique_id=68910ff89eef6
Comments
Post a Comment