What Is Break Statement in c++

We have used a while statement to compute Fibonacci numbers less than 100. The loop exits when the condition at the beginning becomes false. Loops also can be exited at any point through the use of a break statement.

When we want exit from loop then we used break statemet in c.we can provide a specific condition if condition match then this exit from loop.

Suppose you want to add a series of numbers and you don’t know how many numbers are to be added together.You need some way of letting the program know it has reached the end of the list. In Program 6-2 you use the number zero (0) to signal the end of the list.Note that the while statement begins with

while (1) {

The program will loop forever because the while will exit only when the expression 1 is
zero.The only way to exit this loop is through a break statement.When we see the end-of-list indicator (zero), we use the statement:

if (item == 0)
break;

to exit the loop.

Simple example of Break in c++

#include<iostream>
using namespace std;
int main(){
int i;
clrscr();
for(i=1;i<=5;i++)
{
if(i==4)
{
break;
}
cout<<“Welcome %dn”,i;
}
cout<<“Exit from loop here”;
return0
}

Output:
welcome 1
welcome 2
welcome 3
Exit from loop here

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Break Statement in c++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password