C — cycle

Time:2021-1-24

————Resume content————

In C language, there are three loops: while loop, do – while loop and for loop.

 

 

1、 While loop

While: first check whether the conditions for starting the loop are met. When the conditions are met, repeat the statements in the loop until the conditions are not met.

The basic form of while loop is as follows

While (condition){
       ;    ;}

 eg: 1

//Enter any int type integer and judge its number of digits
#include 
int main()
{
    int x;
    int n=0;
    scanf("%d",&x);
    n++;
    x /=10;
    while (x > 0){
        n++;
        x /=10;
    }
     printf("%d\n",n);
    return 0;}

eg: 2

//Determine the number of digits entered
/*
1. User input X; 
2. Initialize n to 0; 
3. X = x / 10, remove bits; 
4.n++;
5. If x > 0, return to 3;
6. Otherwise n is the result; 
*/ 
#include 
int main()
{
    int x;
    int n=0;
    scanf("%d",&x);
    n++;//n=1
    X / = 10; // take the integer quotient of the input number, and the obtained x is put into the while loop 
    When (x > 0) {// judge whether x is greater than zero, and execute the statement in braces if the condition is met  
        n++;//n=2
        x /=10;
    }
     printf("%d\n",n);
    return 0;
 }

eg: 3

//Enter a number to calculate factorial (while method) # include 
int main()
{
    Int n; // define variable n as the number of inputs 
    Scanf (% d ", & n); // read in the input number 
    Int fact = 1; // define the variable fact to save the result of the program (initialization) 
    
    Int i = 1; // define variable I as loop condition (initialization) 
    while (i <= n){
        fact *= i;
        i ++;
    }
    printf("%d!=%d\n",n,fact);
    return 0;
 }

 

2、 Do while loop

Do while: do not check before entering do while, but check whether the conditions of the cycle are met after executing one cycle. If it is met, the next cycle will continue. If not, the cycle will end.

Do while loop must be executed at least once!

Basic form:

do 
{
;
;
}While(); // there must be a bracket after the while brace

 

The difference between while loop and do while loop: while loop is to judge the condition first, do while loop is to judge the condition after executing a round of loop, no matter whether the condition meets do while, it will be executed at least once,

The while loop is executed when the condition is satisfied, but not the end loop.

 eg: 

//Judge any digit (do - while method)
#include 
int main()
{
    int x;
    int n=0;
    scanf("%d",&x); 
    do
    {
        x /=10;
        n ++;
     } while( x >0);
     printf("%d\n",n);
    return 0;
 }

 

3、 For loop

The for loop is similar to a counting loop. It sets a counter, initializes it, and then repeatedly executes the loop body before the counter reaches a certain value. At the same time, each cycle is executed, and the counter value is adjusted in a certain step.

For (initial action, condition, action of each round) {;;}

Routine: when doing summation program, the variable recording the result should be initialized to 0; while when doing quadrature program, the variable recording the result should be initialized to 1

 

While loop and for loop are equivalent, any for loop can be transformed into while loop

//While loop and for loop are equivalent, any for loop can be transformed into while loop

for (int i= 1, i <=n, i ++){
    fact  *= i;
}


            |||||||
            |||||||


int i = 1;
while ( i <=n ){
    fact *=i;
    i ++;
}

eg:

//Enter a number to calculate the factorial (for method) 
#include 
int main()
{
    int n;
    scanf("%d",&n);
    int fact = 1;
    
    Int i = 1; // define the initial value of variable I as 1 
    for (i=1; i<=n; i++ ){
        //I = 1 is the initial condition, I < = n is the condition for the loop to continue, and I + + is the thing to do in each round of the loop 
        /*For example, the variable n I entered earlier is equal to 4, when I execute to the for loop. First, the computer takes the variable i = 1 as the initial value 
        If I is less than or equal to 4, the loop will be executed, that is, the statement block of the loop body will be executed, and then I + + will be executed, otherwise, the loop will be jumped out*/ 
        fact *= i;
    }
    printf("%d!=%d\n",n,fact);
    return 0; 
}

 

Summary: if the number of program cycles, there is a fixed number of use for;

If it has to be executed once, use do – while;

Use while in other cases;

 

 

————End of recovery————

Recommended Today

Go carbon version 1.2.0 has been released to improve and optimize the multi scenario support for orm

Carbon is a lightweight, semantic and developer friendly golang time processing library, which supports chain call, lunar calendar, Gorm, xorm and other mainstream orm If you think it’s good, please give it to a star github:github.com/golang-module/carbon gitee:gitee.com/go-package/carbon Installation and use //Using GitHub Library go get -u github.com/golang-module/carbon import ( “github.com/golang-module/carbon”) //Using gitee Library go get […]