Programming Fundamentals
C++ break & continue Statement
11/10/2019
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
by
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: Shehzadch49@yahoo.Com
1
C++ break and continue Statement
• In C++, there are two statements break; and continue; specifically to alter the
11/10/2019
normal flow of a program.
• Sometimes, it is desirable to skip the execution of a loop for a certain test
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
condition or terminate it immediately without checking the condition.
For example:
• You want to loop through data of all aged people except people aged 65. Or,
you want to find the first person aged 20. In scenarios like these, continue; or
a break; statement is used.
2
C++ break Statement
The break; statement terminates a loop (for, while and do..while loop) and
a switch statement immediately when it appears.
11/10/2019
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Syntax of break
break;
In real practice, break statement is almost always used inside the body of
conditional statement (if...else) inside the loop.
3
How break statement works?
Programming Fundamentals by
11/10/2019
4
SHEHZAD LATIF (03134797617)
Example 1: C++ break
C++ program to add all number entered by user until user enters 0.
#include <iostream> else
using namespace std; {
11/10/2019
int main() { // terminates the loop if number equals 0.0
float number, sum = 0.0; break;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
// test expression is always true }
while (true) }
{ cout << "Sum = " << sum;
cout << "Enter a number: "; return 0;
cin >> number; }
if (number != 0.0)
{
5
sum += number;
}
Output
11/10/2019
Enter a number: 4
Enter a number: 3.4
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
6
Description
o In the above program, the test expression is always true.
11/10/2019
o The user is asked to enter a number which is stored in the
variable number. If the user enters any number other than 0, the
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
number is added to sum and stored to it.
o Again, the user is asked to enter another number. When user
enters 0, the test expression inside if statement is false and body
of else is executed which terminates the loop.
o Finally, the sum is displayed.
7
C++ continue statement
It is sometimes necessary to skip a certain test condition within a loop. In such
11/10/2019
case, continue; statement is used in C++ programming.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
Syntax of continue
continue;
In practice, continue; statement is almost always used inside a conditional
statement.
•
8
Working of continue Statement
Programming Fundamentals by
11/10/2019
9
SHEHZAD LATIF (03134797617)
Example 2: C++ continue
C++ program to display integer from 1 to 10 except 6 and 9.
#include <iostream>
using namespace std;
11/10/2019
int main()
{
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}
return 0;
} 10
Output
1 2 3 4 5 7 8 10
11/10/2019
In above program, when i is 6 or 9, execution of statement
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
cout << i << "\t";
is skipped inside the loop using continue; statement.
11