The three basic program structures of C language are: 1. Sequential structures, such as expression statements, function call statements, and compound statements; 2. Selection structures, such as if statements; 3. Loop structures, such as for statements , while statement, do while statement.
1. Sequential structure
Expression statement, empty statement, function call statement, compound statement
(related video Share: java video tutorial)
Program example:
Input an uppercase letter from the keyboard and request to use lowercase letters for output.
#include<stdio.h> int 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; }
2. Selection structure
if statement program example:
Input three real numbers a, b, c, and output these three in order from small to large algebraic values number.
#include<stdio.h> int 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; }
3. Loop structure
while statement, do while statement, for statement
for statement example:
Calculation: 1 2 3... .. 100.
#include<stdio.h> int main() { int i,x=1; for(i = 2;i <= 100;i++) { x+=i; } printf("%d\n",x); return 0; }
Related recommendations: C language tutorial
The above is the detailed content of What are the three basic program structures of C language?. For more information, please follow other related articles on the PHP Chinese website!