The break and continue statements. Features of use. Examples of using
Contents
- 1. Features of using the break operator
- 2. Features of using the continue operator
- 3. The break statement. Examples of tasks solving using loops
- 4. The continue statement. Examples of solving of tasks
- 5. How does the break statement work in the case of nested loops? Example
- 6. Is it possible use break and continue statements in an if statement?
- Related topics
Search other websites:
1. Features of using the break operator
In C++, there are break and continue statements that are used to control the progress of a looping process. These operators can only be applied in the body of the loop.
The break statement is intended to artificially interrupt execution:
- sequences of iterations in for, while or do-while loop statements;
- sequences of statements in a switch statement.
Most often, the break statement is used in conjunction with the if condition statement. In this case, some condition is checked, and if successful, the break statement is called.
Example.
In the following example, when the variable i reaches the value 3, the loop is exited unconditionally by calling the break statement. The break call is demonstrated for all kinds of operators.
#include <iostream> using namespace std; void main() { int i, j; // The break statement // 1. Loop for cout << "Loop for:" << endl; for (i = 0; i < 5; i++) // loop i { if (i == 3) break; // exit from the for loop // this statement is executed cout << "i = " << i << endl; } // 2. Loop while cout << "Loop while:" << endl; i = 0; while (i < 5) { if (i == 3) break; // exit from the while loop cout << "i = " << i << endl; i++; } // 3. Loop do-while cout << "Loop do-while:" << endl; i = 0; do { if (i == 3) break; // exit from the do-while loop cout << "i = " << i << endl; i++; } while (i < 5); }
The result of the program
Loop for: i = 0 i = 1 i = 2 Loop while: i = 0 i = 1 i = 2 Loop do-while: i = 0 i = 1 i = 2
⇑
2. Features of using the continue operator
The continue statement is intended to go to the next iteration of the loop. If the operator continue is encountered in the body of the loop, then:
- the execution of the current iteration is stopped;
- the transition to the next iteration of the loop occurs.
The continue statement can be used in all types of loops: for, while, do-while. In most cases, the continue statement is called when some condition in the if statement is met.
Example. The example demonstrates the use of the continue statement for all kinds of C++ loops. In the task, when the variable i reaches the value 3, the transition to the next iteration occurs using the continue statement.
#include <iostream> using namespace std; void main() { int i, j; // The continue statement // 1. Цикл for cout << "Loop for:" << endl; for (i = 0; i < 5; i++) // loop i { if (i == 3) continue; // go to next iteration // this statement is executed cout << "i = " << i << endl; } // 2. Loop while cout << "Loop while:" << endl; i = 0; while (i < 5) { if (i == 3) { i++; // increase the counter - required continue; // go to next iteration } cout << "i = " << i << endl; i++; } // 3. Loop do-while cout << "Loop do-while:" << endl; i = 0; do { if (i == 3) { i++; // increase counter - required continue; // go to next iteration } cout << "i = " << i << endl; i++; } while (i < 5); }
Program result
Loop for: i = 0 i = 1 i = 2 i = 4 Loop while: i = 0 i = 1 i = 2 i = 4 Loop do-while: i = 0 i = 1 i = 2 i = 4
⇑
3. The break statement. Examples of tasks solving using loops
3.1. Calculating the sum of numbers entered from the keyboard. Loop for
The user enters numbers from the keyboard. The end of the entry is the number 0. Calculate the sum of the numbers that the user enters. Use the for and break statements in the program.
The solution to the problem in C++ is as follows
#include <iostream> using namespace std; void main() { // Calculating the sum of numbers entered from the keyboard int number, summ = 0; cout << "Enter numbers (0-exit):" << ::endl; // eternal cycle for (;;) { // input the number cout << "number = "; cin >> number; // if you enter 0, then exit the loop if (number == 0) break; // increase the summ by the number summ += number; } // display the summ cout << "summ = " << summ << endl; }
⇑
3.2. Calculating the value of the number π by the formula
Calculate the value of π using the formula with a given precision.
#include <iostream> using namespace std; void main() { // Calculate the value of pi with a given precision // 1. Declare internal variables int n; // the number of decimal places double eps; // precision int i, k; double pi; // result double t; // 2. Enter the number of decimal places cout << "Enter the number of decimal places: n = "; cin >> n; // 3. Formation of precision eps = 1; i = 0; while (i < n) { eps /= 10; // divide by 10 i++; } // 4. The loop of calculation of Pi pi = 1; i = 1; k = 1; t = 0; while (1) // eternal loop { t = pi; i = i + 2; k = -k; pi = pi + k * 1.0 / i; // checking if the desired accuracy has been achieved if (fabs(pi - t) < eps) break; // if accuracy is reached, exit the loop } // 5. Display the result pi = 4 * pi; cout << "pi = " << pi << endl; cout << "i = " << i << endl; }
The example uses the break statement to exit the while loop.
⇑
4. The continue statement. Examples of solving of tasks
4.1. Calculating the sum of the elements of a sequence
Given a sequence of real numbers, which ends with the number 0. Calculate the sum of the elements of a sequence that have values in the range [-5; 5].
The solution of the task in C++ is as follows
#include <iostream> using namespace std; void main() { // Calculating the sum of numbers within specified limits // end of input - value 0 const double MIN = -5.0; // lower limit const double MAX = 5.0; // upper limit double number; // the entered number double summ = 0; // specified summ cout << "Enter numbers (exit - 0):" << ::endl; // Loop do-while do { // number input cout << "=> "; cin >> number; // if the number is out of range if ((number < MIN) || (number > MAX)) continue; // then go to the next iteration of the loop // calculate the summ if the correct number is entered summ += number; } while (number != 0); // numbers are entered up to number 0 // display the summ cout << "summ = " << summ << endl; }
⇑
4.2. Calculating the sum of array elements according to a condition
Calculate the sum of the array elements in paired positions. Since in C ++ array elements are numbered starting from 0, we will consider positions 1, 3, 5, etc. as paired.
The solution to the problem using the continue operator is as follows.
#include <iostream> using namespace std; void main() { // Вычислить сумму элементов массива, placed на парных позициях // 1. Declare internal variables const int MAX = 10; // maximum possible number of elements in the array double A[MAX]; // specified array int n; // the number of elements in the array double summ; // required sum // 2. Enter the number of elements in the array cout << "Enter number of items in the array (1..10): "; cin >> n; // 3. Checking if the data is entered correctly if ((n < 1) || (n > MAX)) return; // 4. Input data in the array cout << "Enter the array:" << endl; for (int i = 0; i < n; i++) { cout << "A[" << i << "] = "; cin >> A[i]; } // 5. Calculating the sum summ = 0; for (int i = 0; i < n; i++) { // checking if the position is paired if (i % 2 == 0) continue; // if the position is paired, then go to the next iteration summ = summ + A[i]; // if the position is unpaired, calculate the sum } // 5. Display the sum cout << "summ = " << summ << endl; }
⇑
5. How does the break statement work in the case of nested loops? Example
In the case of nested loops, the lowest level loop is exited. In the following example, the nested loop on j will never run to the end because it contains a break statement.
#include <iostream> using namespace std; void main() { int i, j; // If loops are nested for (i = 0; i < 5; i++) // Loop i { for (j = 0; j < 5; j++) // Loop j { if (j == 0) break; // exit from the loop by j cout << "j = " << j << endl; } // this statement is executed cout << "i = " << i << endl; } }
The result of the program
i = 0 i = 1 i = 2 i = 3 i = 4
⇑
6. Is it possible use break and continue statements in an if statement?
No, it is not. However, the break statement can also be used as a compound of switch statement.
If you try to use the break statement in the if construction, the compiler will generate an error with the following message
a break statement may only be used within a loop or switch
If you try to call the continue operator in the if structure, the compiler will generate an error with the following message
a continue statement may only be used within a loop
⇑
Related topics
⇑