About those things break and continue

the difference

break statement, it can not only escape from the "loop", you can also jump switch.
break can only be used in both cases. break statement can not be used for any other statement other than the loop and switch statements.
Whether for loop, or a while loop, or a do ... while loop, can be used to break out of, but can only break out of one cycle. When there are multiple layers of nested loops, break only escape from the "package" that its innermost layer of the cycle, the cycle time can not be out of all.

Similarly, multi-layer switch nested program, break out of it only in its distance from the nearest switch. But it is rare nesting multi-layer switch.

continue its role as the end of this cycle, i.e., skip the next loop statement has not been performed, and then determines whether to perform the next cycle.

Differences continue statement and break statement is, continue statement only the end of this cycle, instead of terminating the cycle. The break statement is the end of the whole cycle, conditions are no longer judged execution of the loop is established. Also, continue to use only in the loop, that can only be used for, while and do ... while in addition continue not be used in any statement.

Examples

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i=0;
	while(1)
	{
		i++;
		if(i<10){
			printf("i<10,continue....i=%d\n",i);
			continue;
		}

		printf("i++...i=%d\n",i);
		if(i>10){
			printf("i>10 break...i=%d\n",i);
			break;
		}
	}
	printf("hello world...\n");
	system("pause");
}

Results of the:
Here Insert Picture Description

The above test code i <10 performs the continue statement, the latter printf ( "i i =% d \ n ++ ...", i) is not performed but continues to judge loop condition performed i ++ operations until i> before continuing after = 10 the latter continue to perform statement code.

Guess you like

Origin blog.csdn.net/yuewen2008/article/details/94760747