Разница между операторами continue и break в C ++
Опубликовано: 4 Декабря, 2021
Break и continue - это операторы того же типа, которые специально используются для изменения нормального потока программы, но между ними есть некоторая разница.
break statement: the break statement terminates the smallest enclosing loop (i. e., while, do-while, for or switch statement)
continue statement: the continue statement skips the rest of the loop statement and causes the next iteration of the loop to take place.
пример, чтобы понять разницу между оператором break и continue
// CPP program to demonstrate difference between // continue and break #include <iostream> using namespace std; main() { int i; cout << "The loop with break produces output as:
" ; for (i = 1; i <= 5; i++) { // Program comes out of loop when // i becomes multiple of 3. if ((i % 3) == 0) break ; else cout << i << " " ; } cout << "
The loop with continue produces output as:
" ; for (i = 1; i <= 5; i++) { // The loop prints all values except // those that are multiple of 3. if ((i % 3) == 0) continue ; cout << i << " " ; } } |
Выход:
The loop with break produces output as: 1 2 The loop with continue produces output as: 1 2 4 5