C++ Break and Continue statement

In this article you will learn C++ break and continue statements and will learn when and how to use them efficiently.

There are two statements in C++, break; & continue; for making program more efficient.

Both statements shows their work by their names.

Continue statement is use when you want to skip some specific conditions & except that conditions program will continue.

Break statement is use when you desirably skip the execution of a part of your program for specific test condition & when break; statement appears loop will terminate.

For example:

You want 1 to 5 values except 3. There is an if condition for value 3 when that if condition will true break; statement will skip the value 3 & after this due to continue; statement 4 & 5 values could be store.

C++ break statement

Break; statement breaks (terminate) loop (for, while & do…while) and switch statement when it appears. Mostly it is used inside the body of if statement that is inside the loop.

Syntax:

break;

Example 1: C++ break statement

learn c++

Output 1:

c++

Output 2:

c++

In above example program, user asked to enter the number for which user want to print table & limit of that table.

When limit is less than 10 table will be print because if condition will true. When the limit exceed to 10 if condition will false & body of if statement will print & loop will terminate due to break; statement. As break; statement will breaks the loop & program will terminate.

C++ continue statement

It is use when you want to skip some specific test condition within a loop & it is almost always use inside a conditional statement.

Syntax:

Continue;

Example 2: C++ continue statement

 

Here when if condition will true execution of statement cout<<i<<endl; will skipped due to continue statement.

Here loop will not terminate due to continue statement, it just skipped the execution of body of if statement when if condition will true.