C++Introduction to C++Learn C++

Learn To Use Break And Continue In Loops With C++

1. Using break in Loops

break statement is used to break code-block in case statements and it is also used to jump out of a loop. In an earlier post, we used break in switch() statements.

For example we can break for() loops, in this loop below we can break in some step before counting all range,

Here output will be 0,1,2,3,4,

we can break while() loops too like this,

or as in same example, we can break in do-while loops,

2. Using Continue in Loops

The continue statement allows you to skip next lines of block code in loops, in another way it breaks one iteration in the loop. If there is specified if-condition in that break, it will continue breaking statements while the condition is maintained.

This example below, will skip printing out 5:

We can use both continue and break

 

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap

Worth reading...
Learn to Use While and Do-While Loops in C++