Personal homepage: Click me to enter the homepage
Column classification: C language elementary C language programming——KTV C language mini-game C language advanced
Welcome everyone to like, comment, and collect.
Work hard together and go to Dachang together.
Table of contents
1.1 The framework of the for loop
1.2.2 The running results are as follows
2.1 The framework of while loop
2.2.2 The running results are as follows
3.2.2 The running results are as follows
Hello! Hello! I am back after many days. Today I mainly explain about loops.
loop statement
Loop statements can be divided into for loops, while loops, do....while loops.
1. for loop
1.1 The framework of the for loop
for (initialization; judgment; operation)
{
code block
}
We can see that the for loop includes an initialization part, a judgment part, an operation part, and a code block part.
We can look at the following examples to help us understand the for loop in more detail.
1.2 Examples:
Print 1 to 10
1.2.1 The code is as follows:
#include <stdio.h>
int main()
{
int i = 1;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
1.2.2 The running results are as follows
1.2.3 Analysis
In for(), i=1 is the initialization part; i<=10 is the judgment part; i++ is the operation part, and printf() is the code block part.
2. while loop
2.1 The framework of while loop
while (judgment)
{
code block
}
We can see that the while loop includes the judgment part of the code block.
We can look at the following examples to help us understand the while loop in more detail.
2.2 Examples:
Print 1 to 10
2.2.1 The code is as follows
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("%d ", i);
i++;
}
}
2.2.2 The running results are as follows
2.2.3 Analysis
i<=10 is the judgment part, and the operation part of while is in the code block part.
3.do...while loop
3.1Do...while loop framework
do
{
code block
}while(judgment);
3.2 Examples
Print 1 to 10
3.2.1 The code is as follows
#include<stdio.h>
int main()
{
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 10);
}
3.2.2 The running results are as follows
3.2.3 Analysis
i<=10 is the judgment part, and the operation part of do...while is in the code block part.