-
continue statement
I have just come across the conitnue statement, I have never before seen it used at all. could someone please verify that my understanding of it is correct. Say we have this code:
Code:
for (//setup of for loop in here)
{
if (condition 1)
{
continue;
}
if (condition 2)
{
//perform some tasks
}
}
Am I correct in thinking that if condition 1 is true, the second 'if' statement will never be performed?
Also what would occur if the code was this?:
Code:
for (//setup of for loop in here)
{
if (condition 1)
{
//perform some tasks
}
if (condition 2)
{
continue;
}
}
Thanks in advance for any help given.
-
If you have a loop (for/while/do) and use continue, the rest of the code within the loop will be skipped and the loop restarts.
So yes, you assumption is correct. What will happen in case 2 is the same as if you didn't place the continue there, since you have nothing after it (you can say that the compiler 'places a continue' at the end of every loop).
This will increase X 1 step at a hand, but only print it when it reaches 10:
Code:
int X = 0;
while(true)
{
X++;
if(X < 10)
{
continue;
}
cout << X << endl;
break;
}
-
-
Well you find the continue keyword in C, Java, and C# as well.
-
I think you've also seen the break statement, it exit the loop, or if it's used in a switch statement it will skip the other cases.
Code:
switch( x )
case 1:
//statements
break;
case 2:
....
....
so if x == 1 then it won't check for x == 2.