Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
for (int i = 1; i <= 5; ++i) {
if (i % 2 == 0) {
continue;
}
std::cout << i;
}
}135
- Expecting
1: that is whatbreakdoes;continueends only the current iteration, and the loop goes on with the next value ofi. - Expecting the loop never to stop: in a
forloop,continuestill runs the step++i, soikeeps growing and the loop ends after5.
- What would the program print if
continuewere replaced withbreak? - Why can
continuemake awhileloop endless when its counter is incremented at the end of the body?