Jim Green Jim Green - 2 months ago 9
C++ Question

C++ continue statement inside recursive function

I am new to C++. My previous understanding is that: "continue" is only used within loops such as for, while and do.

However, when I was reading someone else's codes, I came across a "continue" inside a recursive function.

void recursivefunc(){
......
if(condition1) {
continue;
}
......
if(condition2) {
......
} else {
......
recursivefunc();
}


}

Could anyone please tell me what this continue does here? Will I change the function if I replace "continue" with "return"?

Answer

this is a simple program shows how to use continue:

int a = 10;                   // line 1
while(a--)                    // line 2
{                             // line 3
    if(a % 2)                 // line 4
        continue; // if a is odd number then jump back to line 2 to test again, ignoring all the remaining body of loop. // line 5
    cout << a << ", ";  // if the condition failed: no continue reached so continue unitll the end of loop   // line 6

}                             // line 7