C language foundation - the three generals of the cycle

Foreword: Starting from this article, bloggers will continue to update several important basic grammar knowledge in C language from time to time.


Table of contents

1. What is a cycle

1. Definition of cycle

2. Three major circulation bodies

2. Explanation of the cycle body

1. while loop

2.do while loop

3. for loop

 3. The two major keywords in the loop

1.continue

2.break


1. What is a cycle

1. Definition of cycle

Loop, as the name suggests, is a loop, hahaha, just like that kind of spiral slide, circle by circle, layer by layer, the loop statement will be repeatedly executed under the constraints you set .

2. Three major circulation bodies

  • while           
  • do while
  • for

2. Explanation of the cycle body

1. while loop

The while loop is also called "while loop" , I only loop when you meet my restriction .

The grammatical structure of while:

while(expression)

{

        loop statement;

}

 The expression in () after while is the judgment condition , and only when the expression is true , it will enter the while loop and execute the loop statement.

Simple code display:

//打印1-10的数字
#include<stdio.h>
int main()
{
    int i = 1;
    while(i <= 10)
    {
        printf("%d ",i);
        i++;
    }
    return 0;
}

2.do while loop

Like the while loop, do while will only execute the loop if the expression is true .

The grammatical structure of do while:

do

{

        loop statement;

}while(expression);

The only difference between a do while loop and a while loop is that a do while loop executes at least once , while a while loop may never execute at all .

 As in the above code, the expression is obviously false , but 1 will still be printed out. This is the magic of the do while loop. Why? Because the codes are executed one by one from top to bottom , the judgment condition of do while is after the loop statement , so naturally the loop statement is executed first, and then the judgment is made .

3. for loop

The for loop can be described as the big brother of the three loops , and it is also the most versatile loop body.

Syntax structure of for loop:

for (initialization; judgment expression; adjustment statement)

{

        loop body;

}

 The for loop integrates initialization, judgment expressions, and adjustment statements , and only needs to enter the content you want to loop in the loop body. Its execution sequence is: initial--->judgment--->judgment passed--->execute loop body--->adjust statement--->judgment--->judgment not passed--->end loop .

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

 Here we add some variant usage of for loop:

for( ; ; )

        printf("hehe");

}

 The initialization, judgment statement, and adjustment statement in the for loop can all be omitted . It should be noted that when the judgment statement is omitted , it will always be true by default , that is, print "hehe" in an endless loop.

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

 Given such a nested loop code, how many "hehe" will be printed?

The answer is: 3 .

This is about initialization. After the first inner loop is executed, j = 3. At this time, when the inner loop is executed for the second time, j is still 3 because j has not been initialized to a certain number. , so if the judgment condition is not satisfied, it will not be executed.

    int i = 0;
    for (i = 0; i = 1; i++)
    {
        printf("hehe");
    }

How many "hehe" will be printed for such a code?

The answer is: countless, infinite loop printing .

It should be noted that i = 1 here , is it a judgment statement? No, it is actually an assignment statement , and it assigns 1 to i , so the judgment condition is always true , and it will print in an endless loop. 

 3. The two major keywords in the loop

1.continue

continue , its English meaning is "continue" , when it is used in a loop statement, it will immediately end the current loop , stop the execution of the following statement and restart the loop .

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

Looking at the above code, what will be the output of this code?

 Did you guess right?

That's right, after printing 1,2,3,4 , it will print 5 in an infinite loop . why? When we meet the condition of i==5 , the if statement will be executed, and continue will be a demon at this time , it directly terminates the execution of i++ later , and restarts the cycle . At this time, i is always 5 , and it will always Execute continue , i will no longer be ++, so it will print 5 in an endless loop.

2.break

break , its English meaning is "break" , when it appears in a loop statement, no matter where it is, as long as it is shot, it will directly break the entire loop , the entire loop will not be executed , and the statement behind the loop will be executed directly .

#include<stdio.h>
int main()
{
	int i = 1;
	while (i <= 10)
	{
		printf("%d ", i);
		if (i == 5)
			break;
		i++;
	}
	printf("循环被打破了");
	return 0;
}

The same code, what will it output?

 Yes, when we print to 5 in the loop , the if statement executes, break it , and directly end the entire loop after i = 5 , and then execute the printf statement after the loop .


This knowledge sharing about circulation is coming to an end here. If you have any questions or additions to my sharing, please feel free to comment and send me a private message.

Friends who like the blogger's articles, don't forget to click three times, see you in the next issue!

Guess you like

Origin blog.csdn.net/2303_78442132/article/details/132204303