break and continue in loop

1: First acquaintance

During the execution of the loop, if certain conditions occur, the loop needs to be terminated early. This is a very common phenomenon. Break is provided in C language
and
continue
The two keywords should be included in the loop.

?
break
is used to terminate the loop permanently, as long as
break
is executed, it will jump out of the loop and continue execution.

OK.

?
continue
The function is to skip this cycle
continue
The code behind is in
for
loop sum
while
Something in the cycle

Difference.

Two: break and continue in while loop

Examples of 2.1break:

After printing 1,2,3,4, when i equals 5, the loop is positive
break
Terminates at the place, no longer prints, no longer loops.

so
break
The function is to permanently terminate the cycle, as long as
break
be executed,
break
The outer layer of the loop ends.

After that, if we are in the loop and want to terminate the loop under certain conditions, we can use
break
to achieve the effect we want.

2.2continueExample

continue
It means to continue. Its function in the loop is to skip this loop.
continue
The following code continues

judgment for the next cycle.

The above code, if you put
break
Replace with
continue
What will be the result?

At this point we can analyze it,
continue
Can help us skip a certain loop
continue
The code behind is directly

Go to the judgment part of the cycle and judge the next cycle. If the cycle adjustment is
continue
The latter may cause an infinite loop.

After understanding the above code, you can guess the result of the following code.

When i=0 enters the loop, it increases by one and becomes 1. The first value printed is 1. When i=4, it increases by one and becomes 5. It enters if and is followed by continue, so 5 is not Print.

Three: break and continue in for loop

3.1break example

Actually the same as while
in loop
break one
Sample,
for
in loop
break
It is also used to terminate the loop, regardless of whether the loop still needs

How many times do you want to loop, as long as the execution reaches
break
, the loop is completely terminated, and we go to the code.

#include <stdio.h>
int main()
{
 int i = 1;
 for(i=1; i<=10; i + + )
 {
 if(i==5)
 break;
 printf("%d ", i);
 }
 return 0;
}

break
The function is to permanently terminate the loop. In the future, when a certain condition occurs and we no longer want to continue the loop, we can use break
To be done.

3.2continue example

The above code, if you put
break
Replace with
continue
What will be the result?

#include <stdio.h>
int main()
{
 int i = 1;
 for(i=1; i<=10; i + + )
 {
 if(i==5)
 continue;//This?continue skips the subsequent printing and comes to the adjustment part of i++
 printf("%d ", i);
 }
 return 0;
}

So in
for
in loop
continue
The function is to skip the current loop
continue
After the code, go directly to the loop call

whole part. In the future, when a certain condition occurs and this cycle does not need to perform certain subsequent operations, it can be used?

continue
to fulfill.

Four: break and continue in do while loop

do.while
in the sentence
break
and
continue
role and
while
It’s almost the same in the loop. You can come down and

Test it yourself and experience it.

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

Five: Loop nesting

We learned three types of loops earlier
while
,
do while
,
for
, these three types of loops are often nested together for better solution

The problem is what we call: loop nesting. Here we will look at an example.

5.1 Exercise

Find the prime numbers between 100 and 200 and print them on the screen.

Note: A prime number, also known as a prime number, is a number that can only be divided by 1 and itself.

5.2 Question Analysis:

1.
To find a prime number between 100 and 200, you must first have a number between 100 and 200. You can use a loop to solve this problem.

2.
Suppose you want to determine whether i is a prime number. You need to use numbers between 2 and i-1 to divide i. You need to generate numbers between 2 and i-1. You can also use a loop to solve this problem.

3.
If there is a number between 2 and i-1 that can divide i, then i is not a prime number. If none of them can be divided, then i is a prime number.

5.3 code

#include <stdio.h>
int main()
{
 int i = 0;
 //Cycle production? Numbers from 100 to 200
 for(i=100; i<=200; i + + )
 {
 //Determine whether i is a prime number
 //Loop to produce a number between 2~i-1
 int j = 0;
 int flag = 1;//Assume i is a prime number
 for(j=2; j<i; j + + )
 {
 if(i % j == 0)
 {
 flag = 0;
 break;
 }
 }
 if(flag==1)
 printf("%d ", i);
 }
 return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57172 people are learning the system