The three basic structures in C language programs are: 1. Sequential structure, which refers to execution in program order; 2. Selection structure, which refers to selecting the branch direction based on the judgment result; 3. Loop structure, which refers to a loop Body, you can decide how many times to loop based on the judgment conditions.
#The operating environment of this article: Windows 7 system, Dell G3 computer, C11 version.
There are three types of program structures in C language: sequential structure, selection structure, and loop structure. Next, in the article, we will introduce the usage of these three basic structures in detail, which has a certain reference effect. I hope it will be helpful to everyone.
In C language programs There are three program structures: sequential structure, selection structure (branch structure), and loop structure
Sequential structure:
The sequential structure is one sentence after another from beginning to end. Execute it until the last sentence is executed. As shown below
Example: Enter an uppercase letter from the keyboard and request to use lowercase letters instead
#includeint main() { char x,y; scanf("%c",&x); if(x >= 'A' && x <= 'Z') { y=x+32; } else { printf("this is a erro"); } printf("%c\n",y); return 0; }
Select structure
After arriving at a certain node, the branch direction to be executed will be determined based on the result of a judgment. As shown in the figure below
Example: Enter three numbers, and then arrange them from small to large
#includeint main() { float a,b,c,tmp; scanf("%f %f %f",&a,&b,&c); if(a > b) { tmp=b; b=a; a=tmp; } if(a > c) { tmp=c; c=a; a=tmp; } if(b > c) { tmp=c; c=b; b=tmp; } printf("%5.2f %5.2f %5.2f\n",a,b,c); return 0; }
[Recommended courses:C language tutorial】
Loop structure
The loop structure has a loop body , the loop body is a piece of code. For loop structures, the key is to decide how many times to execute the loop body based on the judgment result;
Example: Calculate 1 2 3 ···· 10
#includeint main(void) { int i, sum; printf("i = %d.\n", sum); for (i=0,sum=0; i<=10; i++) { sum += i; } printf("sum = %d.\n", sum); return 0; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone.
The above is the detailed content of What are the three basic structures in C language?. For more information, please follow other related articles on the PHP Chinese website!