Home>Article>Backend Development> What are the three basic program structures of C language?
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.
#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; }
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.
#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; }
3. Loop structure
while statement, do while statement, for statement
for statement example:
Calculation: 1 2 3... .. 100.
#includeint 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!