ANSWER
Continue is used to jump to the end of an iteration in a loop. It is often used in an if statement within a while or for loop to skip the remainder of the loop code.
Example (note: not the easiest way to write this code):
for ( i=0; i<10; i++ )
{
if ( array[i] <= 0 )
{
continue; // do not add this value to the total
}
total += array[i];
}
Chat with our AI personalities
The continue is another jump statement like the break statement as both the statements skip over a part of code . But continue statement is somewhat different from break.Instead of forcing termination,it forces the next iteration of loop to take place, skipping any code in between.A continue statement will just abandon the current iteration and let the loop start the next iteration.
We use the continue keyword within a loop statement (for, while and do) whenever we wish to start a new iteration of the loop, bypassing any and all remaining statements within the loop.
Use of continue should always be conditional, otherwise none of the remaining statements would ever execute.
There are often alternatives to using continue within a loop, but if the condition can be expressed or processed more efficiently with continue then it makes sense to use it.
The following example demonstrates both unconditional and conditional use of continue within while loops. The final loop demonstrates a possible alternative to using continue.
int x = 10;
while( x-- )
{
continue; // Unconditional.
std::cout << x << std::endl; // Never executes!
}
x = 10;
while( x-- )
{
if( x % 2 )
continue; // Conditional.
std::cout << x << std::endl; // Executes when x is even.
}
x = 10;
while( x-- )
{
if( !(x % 2 )) // Alternative to using continue.
std::cout << x << std::endl; // Executes when x is even.
}
In continue statement, we immediately continue next step through loop In go to statement, we go to in perfect label which we call.
In the C language, the continue statement can only be used within a loop (for, while, or do). Upon execution, control transfers to the beginning of the loop.
If you have a loop in your switch statement or around your switch statement, you can use the continue statement in that. You cannot use a continue statement outside of a loop (do, for, or while).
The continue statement is not actually used when it is the last statement of the body of the loop. Plus: outside any loop it is rarely or never used.
The break statement exits out of the smallest containing loop or switch-case statement. The continue statement transfers control to the next iteration of the smallest containing loop statement.