Four program structures in C language __c language

Source: Internet
Author: User
Tags goto

1. Sequential structure
Sequential structure of the program design is the simplest, as long as the solution to solve the problem in order to write the corresponding statement on the line, its execution sequence is top-down, followed by execution.

For example, a = 3,b = 5, now swap the value of a,b, this problem is like swapping two cups of water, which of course uses a third cup, if the third Cup is C, then the correct procedure is: c = A; a = b; b = c; execution result is a = 5,b = c = 3.

If you change the order, write: a = b; c = A; b = c; then the execution result becomes a = b = c = 5, which is not up to the intended purpose, which is the easiest way for us beginners to make this mistake, so we must pay more attention to it.

Sequential structure can be used independently to form a simple complete program, the common input, calculation, output of the three-step program is sequential structure, such as the calculation of the area of the circle, the program of the sentence sequence is the input circle radius r, the calculation s = 3.14159*r*r, the output circle of the area S.

In most cases, however, sequential structures are part of a program, which, together with other structures, constitutes a complex program, such as a compound statement in a branch structure, a loop body in a loop structure, and so on.

2. Branch structure
Although the sequence structure of the program can solve the calculation, output and other problems, but can not be judged and then selected. The branch structure is used for questions that need to be judged before they are selected.

The execution of a branch structure chooses the execution path based on certain conditions, rather than the physical order in which the statement appears. The key of the design method of the branch structure is to construct the proper branching condition and the analytic program flow, and select the appropriate branch statement according to the different program flow.

The branch structure is suitable for the computation which has the logic or the relation comparison and so on judgment, the design this kind of procedure often must first draw its program flow chart, then writes the source program according to the procedure flow, this does the design analysis and the language separation, causes the question simplification, is easy to understand.

The program flow chart is the process flow chart which is drawn according to the problem analysis.

Learning branching structure is not to be confused by branching nesting, as long as the flow chart is correctly plotted and the functions to be performed by each branch are clarified, the nesting structure is not difficult. Nesting is just a branch and also includes branching statements, not new knowledge, as long as the two branches of understanding, branch nesting is not difficult. I'll introduce several basic branching structures below.

①if (condition)
{
Branch body
}

The branch body in this branch structure can be a statement, at which time "{}" can be omitted, or it can be multiple statements that are compound statements.

It has two branch paths to choose from, one is when the condition is true, the branch body is executed, otherwise the branch body is skipped, then the branching body is not executed. For example: To calculate the absolute value of x, according to the definition of absolute value, we know that when x>=0, its absolute value is unchanged, and x<0 when the absolute value is the inverse of x, so the program section is: if (x<0) x=-x;

②if (condition)
{Branch 1}
Else
{Branch 2}

This is a typical branching structure, if the condition is set up, execute branch 1, otherwise the execution branch 2, branch 1 and branch 2 can be 1 or several statements constitute. such as: To find the root of ax^2+bx+c=0

Analysis: Because when b^2-4ac>=0, the equation has two real roots, otherwise (b^2-4ac<0) there are two conjugate roots. Its procedural paragraph is as follows:

D=b*b-4*a*c; 
if (d>=0) 
{x1= (-b+sqrt (d))/2a; 
X1= (-b-sqrt (d))/2a; 
printf ("x1=%8.4f,x2=%8.4f\n", x1,x2); 
} 
else 
{r=-b/(2*a); 
I =sqrt (d)/(2*a); 
printf ("x1=%8.4f+%8.4fi\n" R, i); 
printf ("x2=%8.4f-%8.4fi\n" r,i); 

③ Nested BRANCH statement: The statement format is:
if (condition 1) {branch 1};
else if (condition 2) {branch 2}
else if (condition 3) {Branch 3}
......
else if (condition N) {branch n}
else {Branch N+1}

Although nested branch statements can solve multiple import and export problems, but more than 3 nested, the statement structure becomes very complex, for the reading and understanding of the program is extremely inconvenient, the proposal is nested within 3 weight, more than 3 heavy can use the following statement.

④switch SWITCH statement: The statement is also a multiple-branch selection statement, exactly which one to execute, depending on the switch settings, that is, the value of the expression and the constant expression match the way.

It is different if...else statements, all of its branches are tied, when the program executes, from the first branch to find, if match, execute the next block, then execute the 2nd branch, 3rd branch ... Block until the break statement is encountered, or if it does not match, to find if the next branch matches.

This statement should pay special attention to the reasonable setting of switch condition and the reasonable application of break statement in application.

3. Circulation structure

Cyclic structure can reduce the workload of repetitive writing of the source program, which is used to describe the problem of repeated execution of an algorithm, which is the most useful program structure in programming, the C language provides four kinds of loops, that is Goto loop, while loop, Do–while Loop and for loop.

Four loops can be used to deal with the same problem, generally they can replace each other, but generally do not advocate using Goto loop, because the order of the forced change of the program will often give the operation of the program to bring unpredictable errors, in learning we mainly learn while, do...while, for three kinds of loops.

The main focus of the three types of cyclic structure learning is to make sure that they are the same and different, so that they can be used in different situations, it is necessary to understand the format and execution sequence of the three loops, and the flow chart of each loop will understand how to replace the use.

You can better understand the role of a while loop by rewriting a program with a for statement, for example. In particular, it should be noted that the loop body should contain the statement that tends to end (that is, the change of the value of the loop variable), otherwise it may become a dead loop, which is a common mistake for beginners.

After completing these three cycles, you should clarify their similarities and differences: when using while and do...while loops, the initialization of the loop variable should precede the loop body, while the For loop is generally in statement 1;

The while loop and the For loop are the first to evaluate the expression, then execute the loop body, and the Do...while loop is the first to execute the loop body after the expression is judged, that is, the Do...while loop body is executed at least once, while the while loop and the for may not execute once.

Also note that these three kinds of loops can be used to break out of the loop, with the continue statement to end the cycle, and goto statements and if the cycle, is not to use the break and continue statements to control.

Sequential structure, branch structure and cyclic structure are not isolated from each other, in the cycle can have branches, sequential structure, branches can also have loops, sequential structure, in fact, no matter what kind of structure, we can be generalized to them as a statement.


4. Modular Program Structure

C Language Modular program structure is implemented with functions, the complex C program is divided into several modules, each module is written into a C function, and then through the main function call function and function call function to achieve a large problem of C program writing.

So often say: c program = Main function + child function. Therefore, the function of the definition, call, the return of values, especially to focus on understanding and application, and through the machine debugging to consolidate.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.