Home > Backend Development > C#.Net Tutorial > What are the three basic program structures of C language?

What are the three basic program structures of C language?

王林
Release: 2020-11-12 11:22:25
Original
63004 people have browsed it

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.

What are the three basic program structures of C language?

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 >= &#39;A&#39; && x <= &#39;Z&#39;)
    { 
        y=x+32;
    }
    
    else
    {
        printf("this is a erro"); 
    }
    printf("%c\n",y);
    return 0;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template