Home > Backend Development > C#.Net Tutorial > What are the three basic constructs used in structured programming in C language

What are the three basic constructs used in structured programming in C language

王林
Release: 2020-07-16 10:14:23
Original
23935 people have browsed it

The three basic structures used in structured programming in C language are: sequential structure, selection structure, and loop structure. Programming with a sequential structure is the simplest. Just write the corresponding statements in the order of solving the problem. Its execution order is top-down and executed sequentially.

What are the three basic constructs used in structured programming in C language

The three basic structures used in structured programming in C language are: sequential structure, selection structure, and loop structure.

(Recommended tutorial: C Language Tutorial)

Related introduction:

1. Sequential structure

Programming of sequential structure It is the simplest, just write the corresponding statements in the order of solving the problem, and its execution order is from top to bottom, one after another.

For example: a = 3, b = 5, now exchange the values ​​​​of a and b. This problem is like exchanging the water in two cups. This of course requires the use of the third cup. If the third The cup is c, then the correct program is: c = a; a = b; b = c; the execution results are a = 5, b = c = 3.

If the order is changed and written as: a = b; c = a; b =c; then the execution result will become a = b = c = 5, which cannot achieve the expected purpose.

2. Selection structure

Although the program with sequential structure can solve calculation, output and other problems, it cannot make judgments and then select. For problems that require judgment first and then choice, the choice structure should be used.

The execution of the selection structure selects the execution path based on certain conditions, rather than strictly following the physical order in which statements appear. The key to the selection structure programming method is to construct appropriate branch conditions and analyze the program flow, and select appropriate selection statements according to different program flows.

The selection structure is suitable for calculations with conditional judgments such as logical or relational comparisons. When designing such programs, it is often necessary to first draw the program flow chart, and then write the source program according to the program flow. In this way, the program Design analysis and language are separated, making the problem simple and easy to understand.

3. Loop structure

The loop structure can reduce the workload of repeated writing of the source program and is used to describe the problem of repeatedly executing a certain algorithm. This is the best way to use the computer's expertise in programming. Program structure, C language provides four types of loops, namely goto loop, while loop, do while loop and for loop.

Four types of loops can be used to deal with the same problem. Generally, they can be replaced with each other. However, it is generally not recommended to use goto loops because forcibly changing the order of the program often brings unpredictable consequences to the operation of the program. mistake.

The above is the detailed content of What are the three basic constructs used in structured programming in C language. For more information, please follow other related articles on the PHP Chinese website!

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