What I want to say about C language loop is:

Why_ does_ it_ work 2021-11-25 15:32:05

Written by freshman Meng Xin c The language can understand the version ( One )_Why_does_it_work The blog of -CSDN Blog

Written by freshman Meng Xin c The language can understand the version ( Two )_Why_does_it_work The blog of -CSDN Blog

Coding for so long , How much do you know about running ?_Why_does_it_work The blog of -CSDN Blog

The cycle runs through the whole c The backbone of language , Let's go back and look at the loop .

while A loop should be treated as a loop , The first article emphasized , It's always understood as being ,do,while The same is true of the cycle, which is to take one step first, and then come back when it comes to how

for Loop is the most used in the loop

for The initialization

for The initialization of is the first step , What does the initialization look like if the loop is nested , In the first article , For loop nesting, you should first distinguish between parent and child loops , It's one step in the father's cycle , The sub cycle should be completed , Proceed to the next parent loop , When looking at the code of loop nesting, the easy problem is to ignore the initialization of sub loops

Take the code in the first article to explain

int main()
Print up to 5 That's ok
Print up to 5 Column
because : Number of columns <= Row number
for(int i = 0; i< 5; i++) {
for(int j = 0; j <= i; j++) {
}return 0;

In this cycle, first   i=0,j Perform a loop initialization 0 And then it's over , Enter enter \n Enter the next cycle

                                  i=1,j Perform the first cycle initialization 0 Then the cycle is not over , because 1<=1f If the conditions are met, carry out the second cycle and end

And so on , In short, the first step is to initialize , Part II judgment conditions , Step 3: enter the cycle , Step 4 increase or decrease , Step 5 judge , Step 6: whether to continue the cycle or end the cycle , Therefore, initialization is required at the beginning of each cycle , But the complete end of a cycle , Then start over

for The cycle of

about for loop , We use a lot of places , It has been clearly understood that for What it really looks like , about for Loop let's think about it. In fact, each program is carried out step by step , We should think about what he is doing when we face each problem ? What is the loop part of these operations ? It's a repeat plus ? Repeat multiplication and division ? Or repeat ?

So we should first establish the loop body ( Think on a large scale and then on a small scale ) stay for In circulation ,for The biggest feature is that the range is determined and the number of cycles is known

Such as 5+55 I want that cycle to do , Several times , Once , So the condition can only be operated in one step

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

If you count the sum within the first 100 , That is, after a hundred cycles, the operation is how the relationship between each two items enters the next item , then s=a+s

In fact, these are the simplest contents , The next step is loop nesting , I won't elaborate on this in the first article ;


Arrays are also inseparable from loops , Because arrays are annoying , Array in every previous article , To put it bluntly a[], Two dimensional is a[][]( A few lines and columns ), Said a lot about arrays , To put it bluntly a Is a pointer to the first memory space, and the total number of subscripts from the beginning is , To be specific ?

a[10] from a Point to the first space a[0] There are ten spaces at the beginning ( In fact, what the pointer has been fooling people is a[10] and a[0] and i)

In the loop of the array , The work of arrays is to repeatedly open up space , So his operation for The number in the loop does not refer to ,for Loop means how much space there is , Take a look at the code

This is the code in the first article , You can go back and have a look

#include <stdio.h>
int main() {
int i, a[10];
for (i = 0; i <= 9; i++)
a[i] = i;
for (i = 9; i >= 0; i--)
printf("%d", a[i]);
return 0;

In this code , First, define a variable of array construction type , So this global variable means that such a large space has been opened up in this program , Let's first open up the space we want to use for the array , There is the first cycle , Because we emphasize very much in the first article , The first space is from 0 Start , So we for The number of loop initialization is 0; It takes ten spaces, so <10 That's enough. Then we assign values to each space , There are assignment operations for each step , After ten cycles , Every small memory space of this array is completed , There is a[0]=0;a[1]=1, The purpose of our first article of this code is to output backwards , So we don't open up an address at this time , But array values , So let's change each value from 9 Start outputting backwards , We got it a[i] The value of each item

Actually, in my opinion , Array is a very convenient tool , In fact, it is written in standard. I think it should be completed with pointer code , Because the array itself is a pointer , Each of it is an address , Just because a The reason for this is to make it the first place a[0], When we use array space or address, we should add value operation * Or take the address operation symbol &, But the array itself simplifies it

The same is true for two-dimensional arrays , Take a part of that code and see if you have a different understanding of arrays

#include <stdio.h>
int a[10][10];
int main() {
int n, m;
printf(" Each row and column is controlled as :");
scanf("%d,%d", &n, &m);
printf(" The two-dimensional array is :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
int x, y;
printf(" Want to know :\n");
scanf("%d,%d", &x, &y);
printf(" This element is ");
printf("%d", a[x - 1][y - 1]); // Array int a[m][n] The element at the maximum range is a[m–1][n–1].
return 0;

Same ,scanf and = Are assignment operations

We continue python Learning from ,python Also entered the content of the cycle , So go back and review

Please bring the original link to reprint ,thank
Similar articles