Home > Common Problem > body text

What are the three basic structures in C language?

清浅
Release: 2021-04-18 14:33:48
Original
127180 people have browsed it

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.

What are the three basic structures in C language?

#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.

What are the three basic structures in C language?

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

What are the three basic structures in C language?

Example: Enter an uppercase letter from the keyboard and request to use lowercase letters instead

#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

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

What are the three basic structures in C language?

Example: Enter three numbers, and then arrange them from small to large

#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

[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;

What are the three basic structures in C language?

Example: Calculate 1 2 3 ···· 10

#include <stdio.h>

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!