C language basics: detailed introduction to four program Structures

Source: Internet
Author: User

The following describesCThe four program structures of the language.

1) Ordered Structure

The program design of the sequential structure is the simplest. You only need to write the corresponding statements in the order of solving the problem. The execution sequence of the statements is from top to bottom and executed in sequence.

For example, a = 3, B = 5, and the values of a and B are now exchanged. This problem is like switching two cups of water. Of course, the third cup is used, if the third cup is c, the correct procedure is:

 
 
  1. c = a;   
  2. a = b;   
  3. b = c;  

The execution result is a = 5, B = c = 3. If you change the order, write it as follows:

 
 
  1. a = b;   
  2. c = a;   
  3. b =c;  

Then the execution result becomes a = B = c = 5, which cannot achieve the expected purpose. Therefore, beginners are most likely to make this mistake. The sequential structure can be used independently to form a simple and complete program. Common Input and computing programs and three-step output programs are sequential structures, such as calculating the area of the circle, the statement sequence of the program is the input circle radius r, calculation s = 3.14159 * r, and output Circle area s.

However, in most cases, the sequential structure is a part of the program, which forms a complex program together with other structures, such as the compound statement in the branch structure and the loop body in the loop structure.

2) Branch Structure

Although the sequential structure program can solve problems such as computing and output, it cannot make judgments and then choose. The branch structure should be used for determining and selecting the branch first. The execution of the branch structure selects the execution path based on certain conditions, rather than strictly following the physical order of statements. The key to the program design method of the branch structure is to construct appropriate Branch Conditions and analyze program flows, and select appropriate branch statements based on different program flows.

The branch structure is suitable for computing with conditions such as logical or relational comparison. When designing such programs, you must first draw the program flowchart and then write the source program according to the program flow, in this way, the program design analysis is separated from the language, making the problem simple and easy to understand. The program execution flow chart is drawn based on the problem-solving analysis.

To learn the branch structure, do not be confused by the nested branch. As long as the flowchart is correctly drawn and the functions to be executed by each branch are clarified, the nested structure is not difficult. Nesting is nothing more than a branch statement. It is not a new knowledge. As long as you have a clear understanding of the dual branch, it is not difficult to nest the branch. Below I will introduce several basic branch structures.

① If (condition) {branch body}

In this branch structure, the branch body can be a statement. At this time, "{}" can be omitted, or multiple statements can be compound statements. It has two branch paths. One is to execute the branch body when the condition is true. Otherwise, the branch body will not be executed when the branch body is skipped. For example, to calculate the absolute value of x, according to the definition of the absolute value, we know that when x> = 0, its absolute value remains unchanged, while when x <0, its absolute value is the inverse Number of x, therefore, the program segment is: if (x <0) x =-x;

 
 
  1. If (condition)
  2. {Branch 1}
  3. Else
  4. {Branch 2}

This is a typical branch structure. If the condition is true, execute branch 1. Otherwise, execute branch 2. Both branch 1 and branch 2 can be composed of one or several statements.

For example, find the root of ax ^ 2 + bx + c = 0.

Analysis: When B ^ 2-4ac> = 0, the equation has two solid roots. Otherwise, B ^ 2-4ac <0) has two composite roots. The program segment is as follows:

 
 
  1. main()  
  2. {  
  3. int a,b,c,d,x,y;  
  4. printf("Please put the number of a,b&c from the quadratic equation of one variable one by one\n");  
  5. scanf("%d%d%d",&a,&b,&c);  
  6. d=b*b-4*a*c;  
  7. if(d<0)  
  8. {  
  9. printf("NO Root!Wrong!\n");  
  10. }  
  11. else 
  12. {  
  13. y=-b-sqrt(d)/2*a;  
  14. x=-b+sqrt(d)/2*a;  
  15. printf("The 1st equation root=%d\nThe 2nd equation root=%d",y,x);  
  16. }  

③ IF nested branch statement:

The statement format is:

 
 
  1. If (condition 1) {branch 1}
  2. Else if condition 2) {branch 2}
  3. Else if Condition 3) {Branch 3}
  4. ......
  5. Else if condition n) {branch n}
  6. Else {branch n + 1}
  7. FOR nesting, the statement format is:
  8. For (Initial Value A; range A; Step)
  9. {
  10. For (Initial Value B; range B; Step B)
  11. {
  12. Loop body
  13. }
  14. }

FOR nested example: 9-9 multiplication table

 
 
  1. main()  
  2. {  
  3. int a,b,c;  
  4. for(a=1;a<=9;a++)  
  5. {  
  6. for(b=1;b<=a;b++)  
  7. {  
  8. c=b*a;  
  9. printf("%dx%d=%d ",b,a,c);  
  10. }  
  11. printf("\n");  
  12. }  

Although nested branch statements can solve the problem of multiple entrances and exits, the statement structure becomes very complicated after more than three duplicates are nested, and it is extremely inconvenient to read and understand the program, it is recommended that the values be nested within three, and the following statement can be used if the values exceed three.

④ Switch statement

This statement is also a multi-branch selection statement. It depends on the switch setting, that is, the path where the expression value matches the constant expression. It is different from if... Else statement. All its branches are parallel. When the program is executed, it starts to look up from the first branch. If they match, execute the subsequent block, and then execute the 2nd branch, 3rd branch ...... Until the break statement is encountered. If not, check whether the next branch matches. When using this statement, pay special attention to the reasonable setting of the switch conditions and the rational application of the break statement.

3) loop structure:

The loop structure can reduce the workload of repeated Writing of source programs and describe the problem of repeated execution of a certain segment of algorithms. This is the program structure that best utilizes computer expertise in programming. The C language provides four loops, that is, goto loop, while loop, do? Cwhile loop and for loop. The four loops can be used to deal with the same problem. Generally, they can be replaced by each other, but the goto loop is generally not recommended, forced changes to the program sequence often lead to unexpected errors in program running. in learning, we mainly learn while, do... The while and for loops.

The most common three types of cyclic structure learning focus on figuring out their similarities and differences so that they can be used in different scenarios. This requires that the format and execution sequence of the three types of loops be clear, after understanding the flowchart of each loop, you will understand how to replace and use it. for example, you can re-compile a program with the for statement to better understand the functions of the while loop. Note that the loop body should contain the statement that tends to end, that is, the change of the variable value of the loop). Otherwise, it may become an endless loop, which is a common error for beginners.

After learning these three cycles, we should clarify their similarities and differences: Use the while and do... During the while loop, the initialization operation of the loop variable should be prior to the loop body, while the for loop is generally carried out in statement 1. Both the while loop and the for loop determine the expression first, and do... The while LOOP first executes the loop body and then judges the expression, that is, do... The while LOOP body is executed at least once, while the while loop and for may not be executed at once.

Note that all three cycles can jump out of the loop with the break statement, and end the loop with the continue statement, while the loop composed of the goto statement and the if statement, the break and continue statements cannot be used for control.

The order structure, branch structure, and loop structure are not isolated from each other. In a loop, there can be branches and order structures, and the branches can also have loops and order structures. In fact, no matter which structure, we can think of them as a statement in a broad sense. In the actual programming process, these three structures are often combined to implement various algorithms and design corresponding programs. However, programming is a big problem, programming is often very long and the structure is repeated, resulting in poor readability and hard to understand. The solution to this problem is to design the C program into a modular structure.

(4) modular program structure

The modular program structure of the C language is implemented by functions. The complicated C program is divided into several modules, and each module is compiled into a C function, then, the main function call function and function call function are used to compile a large-scale C program. Therefore, C program = Main Function + subfunction. Therefore, we should pay special attention to understanding and applying the definition, call, and return of values of functions, and consolidate them through machine debugging.

3. Master some simple algorithms

In fact, a major part of programming is to analyze the problem, find a solution to the problem, and then write the code in the corresponding programming language. This requires understanding of algorithms. According to the syllabus of "C programming", we only need to master some simple algorithms. After mastering these basic algorithms, it is easy to analyze the problem. For example, the exchange of two numbers, the comparison of three numbers, the sort by choice method, and the sort by Bubble Method require us to understand the inherent meaning of these algorithms.

Conclusion: When we grasp the above aspects, as long as the students can overcome the fear, dislike, concentrate on the lectures in class, and do a good job in exercises and computer debugging, the C language is not difficult to learn.

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.