I. Introduction
1.break statement: loop - loop interrupts and stops to exit the current loop;
Flow chart:
2.continue: loop - the next iteration of the loop continues.
Flow chart:
Execution process: immediately result this cycle and judge the cycle conditions. If it is true, enter the next cycle, otherwise exit this cycle.
For example: when I write the code, I go to the toilet and come back to continue to write the code.
Two. Example
Exercise 1: output "hello world"
First: continue
while (true) { Console.WriteLine("Hello World"); continue; } Console.ReadKey();
Output result
Second: break
while (true) { Console.WriteLine("Hello World"); continue; }
Output result
Exercise 2: use while continue to calculate the sum of all integers between 1 and 100 (inclusive), except that they can be divided by 7 integers.
int sum = 0; int i = 1; while (i<=100) { if (i%7==0) { i++; continue; } sum += i; i++; } Console.WriteLine(sum); Console.ReadKey();
Output results
Exercise 3: find all prime numbers in 100
//Find all prime numbers in 100 //Prime / Prime: a number that can only be divided by 1 and the number itself //2 3 4 5 6 7 //7 7%1 7%2 7%3 7%4 7%5 7%6 7%7 6%2 for (int i = 2; i <= 100; i++) { bool b = true; for (int j = 2; j <i; j++) { //Apart from the fact that it's not a prime number, there's no need to continue to extract the remainder. if (i % j == 0) { b = false; break; } } if (b) { Console.WriteLine(i); } } Console.ReadKey(); //6 6%2 6%3
Output results