(Nanny level) An article to help you understand loop statements

C language is a structured programming language. The structure here refers to sequential structure, branch structure, and loop structure.

What we are going to talk about today are loop statements while, for, and do while.

Table of contents

1.while loop statement

1.1 Comparison between while and if

1.1.1 Grammatical structure

1.1.2 Examples   

   

1.2 Execution flow of while syntax

1.3 Practice of while loop

 1.4 while and break in the while statement

1.4.1 break in while statement

1.4.2 while in the while statement

1.5 Practice with while statements

2.for loop statement

2.1 Grammatical form of for loop

 2.2 Execution process of for loop

2.3 for loop practice

2.4 break and continue in for loop

2.4.1 break in for loop

2.4.2 continue in for loop

2.5 Loop control variables of for statement

2.6 Variations of for loop

 2.7 A written test question

3.do while statement

3.1 Grammatical structure of do while

 3.2 Execution process of do while

3.3 do while practice

3.4 do while exercises


1.while loop statement

1.1 Comparison between while and if

1.1.1 Grammatical structure

if(表达式)
    语句;

while(表达式)
    语句;//如果循环体有多个语句,要有程序块{}括起来

 while expression is true, the loop executes the corresponding statement until it is not satisfied Conditions.

1.1.2 Examples   

#include <stdio.h>
int main()
{
    if(1)
        printf("hehe\n");
    //if表达式为真,结果:hehe
    return 0;
}

   

#include <stdio.h>
int main()
{
    while(1)
        printf("hehe\n");
    //while表达式为真,结果为无限循环打印hehe
    return 0;
}

1.2 Execution flow of while syntax

while statement,

1. Enter the judgment expression, judge it to be true, and enter the loop.

2. Execute the statement. 1. Enter a new cycle through continue. 2. Enter a new cycle after executing the remaining statements of while. 3. Break out of the loop.

3. If the judgment expression is true, enter the loop, if it is false, jump out of the loop.

1.3 Practice of while loop

Topic: Print values ​​from 1 to 10 on the screen

#include <stdio.h>
int main()
{
    int i = 1;
    while(i<=10)
    {
        printf("%d\n",i);
        //每次打印完之后,i自己加上1。
        i = i + 1; 
    }
    return 0;
}

 Each time printing is completed, i will be incremented by 1 until i =11 and the loop will be jumped out.

 


 1.4 while and break in the while statement

 In the while loop,

break is used to terminate the loop permanently

continue skips the code behind this loop and goes directly to the judgment part for the next judgment.

1.4.1 break in while statement

See the results of the following code?

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

 i=1 enters the loop, i++ is i=i+1, i = 2, print 2

i=2 enters the loop, i++, i=3, prints 3

i=3 enters the loop, i++, i=4, prints 4

i=4 enters the loop, i++, i=5, break jumps out of the loop directly and ends.

 


1.4.2 while in the while statement

See what the result of this code is:

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

 i=1 enters the loop, i++ is i=i+1, i = 2, print 2

i=2 enters the loop, i++, i=3, prints 3

i=3 enters the loop, i++, i=4, prints 4

i=4 enters the loop, i++, i=5, continue skips this loop, if judged to be true, enters the loop again

i=5 enters the loop, i++, i=6, prints 6

i=6 enters the loop, i++, i=7, prints 7

i=7 enters the loop, i++, i=8, prints 8

i=8 enters the loop, i++, i=9, prints 9

i=9 enters the loop, i++, i=10, prints 10

i=10 enters the loop, i++, i=11, prints 11

i=11, the expression is judged to be false and the loop is jumped out.

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

When i=5, continue skips the following code and enters a new cycle

When continue is encountered again, skip the following code and enter a new cycle.

So it’s an endless loop

 


1.5 Practice with while statements

Question: Enter a positive integer and print each digit of this integer in reverse order.

For example:

Input: 1234, Output: 4 3 2 1

Input: 521, output: 1 2 5    

Problem-solving idea: If you want to print bit by bit, just extract each bit of the integer.

  • 1234 extracts 4, 1234 % 10 = 4,
  • 123 extracts 3, 123 % 10 = 3,
  • 12 extracts 2, 12 % 10 = 2,
  • 1extract1, 1 % 10 = 1;
  • Then our question is transformed into, how to extract 123, 12, 12. It is actually simple, as long as 1234 / 10 =123, 123 / 10 =12, 12 / 10 =1;

 Let’s look at the implementation code:

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d",&n);
    while(n)
    {
        printf("%d ",n%10);
        //下面等同于n=n/10;
        n /= 10;
    }
    return 0;
}

 


2.for loop statement

2.1 Grammatical form of for loop

The syntax form of for loop:

for(表达式1;表达式2;表达式3)
    语句;//如果循环体有多个语句,要有程序块{}括起来

Expression 1 is used for loop variableinitialization

Expression 2 is used to judge the loopend condition

Expression 3 for loopAdjustment of variables

Table of expressionGiveTable of expressionFor ; Last apart


 2.2 Execution process of for loop

Expression 1 Initialization

Expression 2 Judgment, judgment determines whether to enter a loop or not

Expression 3 Change the value of the variable


2.3 for loop practice

Topic: Print values ​​from 1 to 10

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

 I come from here for circulation while circulation

for loop

1.Initialize i=0;

2. Change the value of the loop variable, i++ replaces i = i + 1;

What remains unchanged is:

Determine the existence of the condition: i<=10

#include <stdio.h>
int main()
{
    int i = 0;
    while(i<=10)
    {
        printf("%d\n",i);
        //每次打印完之后,i自己加上1。
        i = i + 1; 
    }
    return 0;
}

2.4 break and continue in for loop

2.4.1 break in for loop

break in a for loop will jump directly out of the loop body

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

}

 When i=5, break jumps out of the loop and only prints 1 2 3 4.

 


2.4.2 continue in for loop

What is the result below?

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

}

 The function of continue is to skip the code after this loop and continue to enter a new loop.

According to the execution flow of for, continue will skip the following printf("%d",i), enter expression 3, change the value of loop variable i, then enter expression 2 for judgment, and finally enter the loop this time body.

 

Summary: continue skips this loop, break permanently jumps out of the loop.​ 

2.5 Loop control variables of for statement

suggestion:

1. Do not modify loop variables within the for loop body to prevent the for loop from losing control.

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

}

 

     


2. It is recommended that the value of the loop control variable of the for statement be written in the "front closed and then open range" writing method

int i = 0;
//前闭后开的写法
for(i=0;i<10;i++)
{}

//两边都是闭区间
for(i=0;i<=9;i++)
{}

 Looking at the example below, we see that i<10,10 is the number of elements in the arr array. It makes no sense to change it to i<=9,9.

    int main()
    {
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        int i=0;
1|      for(i=0;i<10;i++)
        {
            printf("%d ",arr[i])
        }

2|      for(i=0;i<=9;i++)
        {
        printf("%d ",arr[i])
        }

3|      for(i=100;i<=200;i++)
        {
            printf();
        }
        return 0;

    }

In the 3| loop, we use closed intervals. We find that whether it is a closed interval or an open interval, it is actually to better understand the constraints, and we choose whichever one is meaningful.

2.6 Variations of for loop

 The initialization in the for loop can be omitted in some cases, for example: there is initialization of i in the front
int main()
{
    int i = 0;
    for(i=0;i<10;i++)
    {
        printf("hehe\n");
    }
    return 0;
}
int main()
{
    int i = 0;
    for(;i<10;i++)
    {
        printf("hehe\n");
    }
    return 0;
}

In the for loop, in some cases, initialization cannot be omitted. For example: the following loop about the variable j needs to be performed three times, and it must be re-initialized before printing hehe three times.

int main()
{
    int i = 0;
    int j = 0;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("hehe\n");
        }
    }
    return 0;
}

 

int main()
{
    int i = 0;
    int j = 0;
    for(;i<3;i++)
    {
        for(;j<3;j++)
        {
            printf("hehe\n");
        }
    }
}

The writing method supported by the C99 standard: write the definition of i in a for loop (MSVC compiler does not support it)

int main()
{
    for(int i=0;i<10;i++)
    {
        printf("hehe\n");
    }
    return 0;
}

 Use an extra variable to control the loop 

int main()
{
    int x,y;
    for(x=0,y=0;x<2 && y<5;++x,y++)
    {
        printf("hehe\n");
    }
    return 0;
}

 2.7 A written test question

How many times does it cycle?

#include<stdio.h>
int main()
{
    int i = 0;
    int k = 0;
    for(i=0,k=0;k=0;i++,k++)
        k++;
             
    return 0;
}

Answer: Loop 0 times, becauseexpression 2 = is not a judgment, but an assignment. k=0 means it is judged to be false. , does not enter the loop.

  • In C language, 0 means false and non-zero means true.

3.do while statement

3.1 Grammatical structure of do while

do
    循环语句;//语句多的时候我们要用程序块{}将语句括起来
while(表达式);

 3.2 Execution process of do while

The difference from whlie is that the conditional expression is after the loop body. Regardless of whether the loop variable symbol meets the condition, we will enter the loop first.

3.3 do while practice

Topic: Print values ​​from 1 to 10

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

    return 0;
}

3.4 do while exercises

What is the printed result of the following code?

#include<stdio.h>
 int main()
{
    int i = 1;
    do
    {
        i++;
        if(i==5)
            continue;

        printf("%d ",i);
    }while(i<=10);

    return 0;

}

 i=1 enters the loop, i++ means i=i+1, i=2, and prints in turn. When i=5, continue skips the following printing and enters the next loop, then i=6,7,8 ,9,10,11. After 11 is printed, the judgment will be invalid and the loop will be jumped out.


What is the printed result of the following code?​ 

#include<stdio.h>
 int main()
{
    int i = 1;
    do
    {
        if(i==5)
            continue;

        printf("%d ",i);
        i++;
    }while(i<=10);

    return 0;

}

 i=1 enters the loop, prints 1, and then adds 1.

i=2 enters the loop, prints 2, and then adds 1.

i=3 enters the loop, prints 3, and then adds 1.

i=4 enters the loop, prints 4, and then adds 1.

i=5 enters the loop, continue jumps out of the loop

i=5 enters the loop, continue jumps out of the loop

i=5 enters the loop, continue jumps out of the loop

So the result is 1 2 3 4 (infinite loop)

 

Guess you like

Origin blog.csdn.net/2302_79491024/article/details/134168618