Detailed explanation and use of three loop statements (for, while, do-while)

For those who are new to programming, they will definitely encounter these three types of loops. The book may be too professional and it will make us feel difficult to understand. Here I will help you with the most concise and clear expression. Understand and learn to use these three cycles.
For the big guys, after reading, you may have new experiences and new discoveries [cute].
At the same time, I will talk about the two small followers of the loop at the bottom (break和continue
Insert picture description here

for loop

This kind of loop should be the most used, and it is also a well-understood loop.

Structure (this is still necessary to understand)

The structure of the for loop is as follows:

for (initialization expression; loop control statement; value-added expression)
{   sequence of statements }

The content in the braces is called the loop body (that is, the content of the loop execution).
Note: The loop body can contain many sentences.
This is usually written in books, but it will make people feel very embarrassed, so let's translate [hee].
Looping is actually doing one thing repeatedly. At the same time, we also need to specify the number of loops (that is, how many times to repeat). Is the process of counting equal to recording the number of times? Looping is like that.

for (count from a few days; count to a few ends; number increase or decrease)
{   what we repeat }

Usage (simple and rude teach you to use)

As for the use, the easiest is to repeat the output of a sentence.

int n=10; // 规定循环多少次
for(int i=0;i<n;i++)
{
    
    
    printf("重复做的事\n");
}

Note that here, I used a temporary variable iTo control the number of loops, and it is defined in parentheses. This has many advantages (better than defining outside for), so I won’t go into details.
At the same time, value-added expressions can not only be written in i++ (i–, i += 2, i * = 3,...) These can be written, according to our own gameplay [hahaha]
Let’s talk about the loop execution first process:

1. First is to execute int i = 0 (this sentence will only be executed once at the beginning of the loop, and will not be executed later)
2. Then execute the loop control statement (i<n)
if the loop control statement is true (this When i<n), execute the loop body (that is, the output statement).
If the loop control statement is false (at this time i>=n), end the loop
3. Execute the value-added expression (i++)
4. Continue from 2. Start to execute down (until the loop control statement is false, exit the loop)

Operation result: (Of course it outputs "repeated things" 10 times, don’t believe you count [proud])
Insert picture description here
There are many things that for loops can do for us...
Pay attention to this point: initialization expressions, loop control statements, value-added expressions , These three positions can be omitted. As for how you want to play, it's up to you. [Hee hee]

int n=10; // 规定循环多少次
int i = 0;
for(;;)
{
    
    
    if(i>=10)
        break;
    printf("重复做的事\n");
    i++;
}

The code written like this has the same effect as above: (If you don't believe me, try it)
Insert picture description here

At the same time, the use of break and continue in conjunction with loops will have very different effects, which I will talk about below.

while loop

This kind of loop is also very common and easy to understand. In fact, it is similar to a for loop, exceptThe initialization condition is written outside, and the value-added expression is written inside

Structure (this is usually written in the book)

The structure of the while loop is as follows:

while (loop control expression)
{   sequence of statements }

Me: This kind of loop is very simple and straightforward. If the loop expression is true, the loop body is executed (as mentioned above). Each time the loop body is executed, it must be judged first, and the loop is ended when the loop expression is false. .
Reader: What is a loop body, say it again?
Author: De Ling [Bi Gongbi Jing]

Insert picture description here

Loop body: all the content in braces can be more than one sentence.

Usage (that's so simple, you will definitely see it)

Generally speaking, the while loop is used for counting down or traversing the number of loops not clear

int n=10; 
while(n>0)
{
    
    
    printf("看到这里的小可爱最帅/最美\n");
    n--;
}

As long as we write the control statement, we will first judge whether the next loop control expression is true before executing the loop body, and continue to execute the loop if it is true, otherwise it will end the loop.
Note: If you forget to change the variable that affects the loop control expression (that is, this n) in the loop body, this loop will become an endless loop (execute all the time)

operation result:
Of course I can’t express my inner compliment 10 times, but the space is limited [humble]

Insert picture description here

do-while loop

Me: This kind of loop is different from the while loop.
Reader: Isn’t this nonsense?

Insert picture description here
I [Broken nose and swollen face]: Don’t fight, I haven’t finished yet.
Reader: Go ahead.
I [Humble, cry]: The while loop will judge before executing the loop body Next (execute the loop control expression), and the do-while loop will execute the loop body before executing the loop control expression (this first loop body is executed directly without judgment)

Structure (this is in the book)

do
{   sequence of statements }while (loop control expression);

Usage (this is it?)

This kind of cycle is not used very frequently, but sometimes it is powerful and suitable.

int n=10;
do
{
    
    
    printf("此时的n=%d\n",n);
    n--;
}while(n>0);

Note: Don't miss the n--, otherwise it will be an endless loop.
When n=0, it will jump out of the loop, because the condition for executing the loop body is n>0

operation result:
Insert picture description here
Reader: I don’t even look at it.
I [Humble]: I was wrong.

Insert picture description here

How to break out of the loop halfway

Sometimes when we execute the loop, we don’t know the specific number of loops, or we want to eliminate a certain special situation. This is the need to use the two small followers of the loop (break and continue)
The code not called a small follower:

for(int i=1;i<=10;i++) //打印1-10这10个数
{
    
    
    printf("%d\n",i);
}

operation result:
Insert picture description here

break (big brother)

Why is break the big brother? Because break said to stop the loop and immediately ended the loop.

break: print the number before 6
Loop: don’t you output 10 numbers
break: close your mouth, I will say the number, and the number that reaches 6 will end for me, not allowed to print

The code called break:

for(int i=1;i<=10;i++) 
{
    
    
    if(i==6)
        break;
    printf("%d\n",i);
}

operation result:
Insert picture description here

Big brother is really big brother! (admire)

continue (brother)

Continue also imagine like the big brother, but the strength is limited, only one loop body can be ended, and the next loop will continue to execute.
continue sighed silently
Insert picture description here

The code called continue:

for(int i=1;i<=10;i++)
{
    
    
    if(i==6)
        continue;
    printf("%d\n",i);
}

operation result:
Insert picture description here
Thank you very much for the little cuties who have read here (may wish to like it before leaving)
Don’t forget to pay attention [Thanks] [Excited]

Insert picture description here
Come on!

Work together!

Keafmd

Guess you like

Origin blog.csdn.net/weixin_43883917/article/details/108693078