Today, the president of our computer Association told me to give my primary school brother Elementary School girls A C-language introduction, their teacher has already told the previous blog we wrote, I intend to tell them the basic structure of C language-cycle, judgment, branch.
Today we will focus on the circular structure.
The cyclic structure is divided into three kinds, namely for, while, Dowhile;
We first say the first: for loop. The format of his code is:
for(判断的数值初始化;判断条件;改变判断数值大小){ 循环语句块;}
For example, let's take an example of the output 1~10 number:
#include<stdio.h>int main(void){ int i; for(i=1;i<=10;i++){ printf("%d\t",i); }}
The result of the program operation is:
1 2 3 4 5 6 7 8 9 10
That is, the first cycle when the I=1 output and run the i=i+1 operation, the second cycle, until i>11.
Let's talk about the use of the while loop:
while(循环判断条件){ 循环语句块; }
We also use the above example to write the program:
#include<stdio.h>int main(void){ int i=1; while(i<=10){ printf("%d\t",i); i++; }}
The result of the program operation remains:
1 2 3 4 5 6 7 8 9 10
His running process and for similar, no longer explained too much.
Now, let's say it's not the same as the two of them. Do-while Loops
First, let's take a look at his structure.
#include<stdio.h>int main(void){ int i=1; do{ printf("%d\t",i); i++; }while(i<=10);}
His results and the above, no longer tell everyone his results, why we say that this cycle and the front of the loop is not the same, because the first two loops are first to judge the running program quickly, and Do-while cycle is the first to run the program quickly, then judge, see if he meets the conditions.
This is the whole loop structure, very simple, but generally they are used in nesting. I need you to delve into this.
The basic structure of C language--cyclic structure