knowledgediarybd.com

Your Virtual Knowledge Campus







Continue and Break Statement In C++

Break and Continue StatementContinue and Break Statement are used  in C++ for controlling  various kind of loop such as for loop, while loop & do..while loop etc.

1.The continue Keyword:

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop.

  1. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement.
  2. In a while loop or do/while loop, flow of control immediately jumps to the Boolean expression.

Syntax of Continue Statement:

continue;

More details see the following figure:

continue

Example:

#include <iostream>
using namespace std;
int main()
{
int count=0,total=0,score;
while(count<2)
{

cout<<"Enter your Score:"<<endl;2
cin>>score;
if((score<0)||(score>100))
continue;
total=total+score;
cout<<"\nTotal score is:"<<total<<endl;
count++;
}
return 0;
}

The output of the above code looks like below:

Enter your Score:10
Total score is:10.
Enter your Score:-5

Enter your Score:101Enter your Score:20

Total score is:30.

2. The break Keyword or Statement:

The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement.

The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax of break statement:

break;

More details see the following figure:

break

Example of break Statement:

#include<iostream>
using namespace std;
int main()
{
int i,array[5]={10,20,30,40,50};
for (i=0;i<5;i++)
{
if (array[i]==30)
break;
else
cout<<"array:"<<array[i]<<endl;
}
}

This would produce the following result:

array[0]=10
array[1]=20

Note:More interested things are waiting to publish..Stay connected to us..

——————-:Thank you:—————–

Related Post or you may felt interest:

  1. Introduction & Fundamental of C++

  2. Code Blocks Compiler Download

  3. Variable In C++

  4. Data Types In C++

  5. Token in c++

  6. C++ Operators,Operands and expression

  7. If, Else If..Else & Switch Statement

  8. For loop while loop&Do while loop in C++

knowledgediarybd.com © 2017
shares