break AND continue IN C++
What are break and continue statements
in C++?
The break and continue statements,
along with goto statement, are termed as the jump statements in C and C++.
These are called so because they enable us to jump the control (i.e. move the
control) from one point in the program to some other point. Where goto statement
can be used to move control anywhere within a function, break and continue are
generally used to modify controls for a loop or switch case.
THE break STATEMENT
A break statement is very often
used in C or C++ program to directly come out of any loop (for, while
or do-while) and switch case. Most of the times
break is used with if-else so that the control moves out of a loop under
certain conditions. Reading below will make the concept clearer.
Syntax : break;
How break works?
The basic rule is that whenever a break statement is encountered, be it inside a loop or switch case, the control immediately comes out of that block (loop or switch case). Often break is used with if-else inside a loop or switch to come out of that loop or switch under the conditions specified in if.
The break
statement takes control out of the innermost loop in which it is present. So,
if there is a nested loop (loop within loop), break will take control
out of the loop in which it is present.
Look at the following representations
to understand the concept better :
A demo program in C++ to show working of break statement :
// Program
to calculate sum of
// maximum 5
integers.
// If
negative integer is entered,
// loop
terminates.
#include <iostream>
using namespace std;
int main()
{
int n, i, sum = 0;
for( i = 1; i <= 5; i++)
{
cout<< “Enter a number : ”;
cin>>n;
if (n >= 0)
sum = sum + n;
else
break;
}
cout<< “Sum is ”<< sum;
cout<< “Value of i is ”<< i;
return 0;
}
Enter a number : 12
Enter a number : 42
Enter a number : -6
Sum is 54
Value of i is 3
Explanation :
In the loop, every time the user is asked to enter a number. The number entered
by the user is stored in n. If the
number is non-negative, it is added to the variable sum and this added value becomes new value of sum. But as soon as the user enters a negative
number, control encounters a break statement and immediately comes out
of the loop. This is evident from the fact that value of i after the
termination of loop is 3 and not 5.
THE continue STATEMENT
The continue statement in C or
C++ is used in loops to ignore a set of statements. Whenever a continue
statement is encountered, the control moves directly to the condition part in while
or do-while and to the update part in for loop.
Syntax :
continue;
How continue works?
The rule is simple. If continue
is encountered anywhere in a for loop, control passes directly to the
update part. If continue is encountered in a while or do-while
loop, control passes directly to the condition part. The general working can be
understood better from the following diagram.
Note that if the update part of while
or do-while loop appear after the continue statement, it is never
actually executed. This results in the condition always being true and loop is
repeated infinite times. In short, the program crashes. So, to remove this
problem, it is always advised to use continue
with for loop, or update the loop variable in while or do-while
before the continue statement.
A demo program in C++ to show working of continue statement :
// Program
to calculate sum of
// maximum 5
integers.
// If
negative integer is entered,
// it is
ignored.
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
for(int i = 1; i <= 5; i++)
{
cout<< “Enter a number : ”;
cin>>n;
if (n < 0)
continue;
sum = sum + n;
}
cout<< “Sum is ”<< sum;
return 0;
}
Enter a number : 12
Enter a number : 42
Enter a number : -6
Enter a number : 50
Enter a number : -32
Sum is 104
Explanation :
In the loop, every time the user is asked to enter a number. The number entered
by the user is stored in n. If the
number entered is non-negative, the if statement is not executed and
control executes the statement sum= sum+n. This statement adds the value of n to sum and the
resulting value is stored in sum. But
if the number entered by the user is negative (i.e. less than zero), continue
statement is executed. This passes the control directly to the update part i++. Thus,
negative numbers are not added into sum.
If you find
something wrong or if you wish to add some information in this article, feel
free to comment on this post or contact us at coding.nkcoder@gmail.com.
Which
topics you want an article on? Comment down on this post.
Do not
stop here. Read all articles related to C++ here.
If you
want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download
the pdf form of this article here.
Post a Comment