Home>Article> What are the three basic structures in C language?

What are the three basic structures in C language?

清浅
清浅 Original
2019-02-22 14:49:42 127161browse

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

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

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

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!

Statement:
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
Previous article:What is TCP protocol? Next article:What is TCP protocol?