break, continue statement

content

break statement

continue statement


break statement

We can use break to break out of a switch statement, and the same goes for loops.

int main()
{
	int i = 10;
	while (i)
	{
		i++;
		if (i == 15)
			break;
	}
    printf("%d", i);
	return 0;
}
int main()
{
	int i = 10;
	for (i = 10;; i++)
	{
		i++;
		if (i == 15)
			break;
	}
    printf("%d", i);
	return 0;
}

The break here is to directly jump out of the loop of this layer and print when i==5 . What if you encounter multiple loops? That is to jump out of the loop where break is located .

continue statement

The continue statement is different from break, and continue skips the code behind this loop . Above:

 

 we're looking at a piece of code

int main()
{
	int i = 10;
	while (i)
	{
		if (i == 5)
			continue;
		i--;
	}
	printf("%d", i);
	return 0;
}

The execution result of this program is an infinite loop . Because the value of i has not changed since i==5.

Guess you like

Origin blog.csdn.net/qq_54880517/article/details/123170577