web123456

break and continue in while

Difference between break and continue

break: Permanently terminate the loop, the part behind the break will no longer be executed, and the while loop will also end.
continue: End this loop, the part after the continued loop will no longer be executed, and the next loop judgment will begin.

#include<>
#include<>
int main()
{
	int i = 1;
	while (i <= 10)
	{
		if (i == 5)
			break;
		printf("%d ", i);
		i++;
	}
	system("pause");
	return 0;
}

The output is 1 2 3 4

#include<>
#include<>
int main()
{
	int i = 1;
	while (i <= 10)
	{
		if (i == 5)
			continue;
		printf("%d ", i);
		i++;
	}
	system("pause");
	return 0;
}

Indestructive loop, output 1 2 3 4 falls into a dead loop when i=5. But if you use the for loop, you won't fall into a dead loop here.

#include<>
#include<>
int main()
{
	int i = 1;
	for (i = 1; i <= 10;i++)
	{
		if (i == 5)
			continue;
		printf("%d ", i);
	}
	system("pause");
	return 0;
}

The output is 1 2 3 4 6 7 8 9 10