break and continue in C++

Repeatedly execute a block of code using the loop control statements in C++. The following are the loop control statements in C++:

  1. break statement
  2. continue statement

C++ break statement

Use the break statement if you want to jump out of a loop. The control passes to the first statement after the loop, since the break statement terminates the loop.

The syntax for the break statement in C++:

The following is an example of implementing the break statement in C++:

Here is the output:

The above C++ code uses a while loop to iterate while i is less than 10. It prints the value of i and increments it. If i becomes greater than 7, the break statement exits the loop. The loop terminates early when i exceeds 7, even though the condition i < 10 is still true.

C++ continue statement

Use the continue statement if you want to pass control to the next iteration of the loop. However, it skips any code in between.

The syntax for the continue statement in C++:

The following is an example of implementing the continue statement in C++:

Here is the output:

The above C++ code iterates from 100 to 114 using a do-while loop. If i is 110, the loop skips the current iteration, increments i, and continues with the next iteration. For all other values of i, it prints i and then increments it. The loop stops when i reaches 115.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C++ Introduction
Call by Value in C++
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading