Оператор continue
Принудительно пропускает оставшуюся часть тела цикла for, range-for, while или do-while.
Используется в тех случаях, когда иным способом пропустить оставшуюся часть цикла с помощью условных операторов неудобно.
Синтаксис
attr (необязательно) continue ; |
Объяснение
Оператор continue вызывает переход, как если бы был оператор goto к концу тела цикла (он может находиться только в теле циклов for, range-for, while и do-while).
Более точно,
Для цикла while он действует как
while (/* ... */)
{
// ...
continue; // acts as goto contin;
// ...
contin:;
}Для цикла do-while он действует как:
do
{
// ...
continue; // acts as goto contin;
// ...
contin:;
} while (/* ... */);Для циклов for и range-for он действует как:
for (/* ... */)
{
// ...
continue; // acts as goto contin;
// ...
contin:;
}Ключевые слова
Пример
#include <iostream>
int main()
{
for (int i = 0; i < 10; ++i)
{
if (i != 5)
continue;
std::cout << i << ' '; // this statement is skipped each time i != 5
}
std::cout << '\n';
for (int j = 0; 2 != j; ++j)
for (int k = 0; k < 5; ++k) // only this loop is affected by continue
{
if (k == 3)
continue;
// this statement is skipped each time k == 3:
std::cout << '(' << j << ',' << k << ") ";
}
std::cout << '\n';
}Вывод:
5 (0,0) (0,1) (0,2) (0,4) (1,0) (1,1) (1,2) (1,4)
См. также
Документация C по continue |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/continue