This is the basis of learning C programming well. C language is very flexible in operation and rich in functions, and the types of operations are far more than other programming languages. Compared with other programming languages, expressions are simpler, such as self-addition, self-subtraction, comma operation, eye operation and so on, which makes expressions simpler. But beginners often find this expression difficult to understand, and the key reason is that they don't fully understand operators and operation sequences. When a variety of different operations form an operation expression, that is, when there are multiple operators in an expression, the priority order and combination rules of operations are very important. In learning, it is not difficult to remember these operations as long as we classify them reasonably and find out the difference between them and the operations learned in mathematics. Some operators will be kept in mind after understanding, and they will be handy to use in the future. Some operators can be temporarily abandoned and remembered later.
First of all, operators should be classified according to different priorities. Operators in C programming can be divided into 15 priority, from high to low, and the priority is 1 ~ 15. Except the second, third and 14 levels, they are all combined from left to right, which determines the operation order of the operators at the same level.
2. Learn the four program structures of C language well
(1) sequence structure
The programming of Sequence structure is the simplest, as long as the corresponding statements are written in the order of solving problems, its execution order is top-down.
For example; A = 3, b = 5. Now exchange the values of A and B. This problem is like exchanging two glasses of water, of course, you need a third cup. If the third cup is C, then the correct procedure is: C = a;; ; a = b; b = c; The execution result is a = 5 and b = c = 3. If the order is changed, it is written as: a = b;; ; c = a; b = c; Then the execution result becomes a = b = c = 5, which fails to achieve the expected purpose. Beginners are most likely to make such mistakes. Sequence structure can be used independently to form a simple and complete program. The common program for inputting, calculating and outputting trilogy is sequence structure. For example, the statement sequence of the program is to input the radius r of the circle, calculate s = 3. 14 159*r*r, and output the area s of the circle. But in most cases, the sequence structure is a part of the program, and it forms a complex program together with other structures, such as compound statements in the branch structure and loop bodies in the loop structure.
(2) Branch structure
Although the program of sequence structure can solve the problems of calculation and output, it can't be judged before selection. For problems that need to be judged before selection, branch structure should be used. The execution of branch structure is to choose the execution path according to certain conditions, not strictly according to the physical order in which statements appear. The key of branch structure programming method lies in constructing appropriate branch conditions and analyzing program flow, and selecting appropriate branch statements according to different program flow. Branch structure is suitable for the calculation of conditional judgment such as logical or relational comparison. When designing this kind of program, it is often necessary to draw its program flow chart first, and then write the source program according to the program flow, so as to separate the program design analysis from the language and make the problem easy to understand. Program flow chart is a program execution flow chart drawn according to problem solving analysis.
Learning branch structure should not be confused by branch nesting. As long as the flow chart is drawn correctly and the function to be executed by each branch is clear, it is not difficult to nest the structure. Nesting is just a branch that contains branch statements, and it is not new knowledge. As long as the double branches are clearly understood, branch nesting is not difficult. Let me introduce some basic branch structures.
If (condition)
{
branch
}
The branch in this branch structure can be one statement, in which case "{}" can be omitted, or it can be multiple statements, that is, compound statements. It has two branch paths to choose from. One is to execute the branch when the condition is true, otherwise, the branch is skipped and not executed. For example, calculate the absolute value of x, according to the definition of absolute value, we know that when x >;; =0, its absolute value remains the same, and x
② If (condition)
{Branch 1}
other
{Branch 2}
This is a typical branch structure. If the condition holds, execute branch 1, otherwise execute branch 2, branch 1 and branch 2, and branch 2 may be composed of 1 or several statements. For example, 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
d = b * b-4 * a * c;
if(d & gt; =0)
{ x 1 =(-b+sqrt(d))/2a;
x 1 =(-b-sqrt(d))/2a;
printf("x 1=%8.4f,x2=%8.4f\n ",x 1,x2);
}
other
{ r =-b/(2 * a);
I = sqrt(-d)/(2 * a);
printf(" x 1 = % 8.4f+% 8.4 fi \ n " r,I);
printf("x2=%8.4f-%8.4fi\n"r,I)
}
③ Nested branch statement: its statement format is:
If (condition 1){ branch1};
Else if (condition 2){ branch 2}
Else if (condition 3){ branch 3}
……
Else if (condition n){ branch n}
Else {n+ 1 branch}
Nested branch sentences can solve the problem of multiple entrances and exits, but after more than three times of nesting, the sentence structure becomes very complicated, which is extremely inconvenient for reading and understanding the program. It is recommended to nest within the triple, and the following statement can be used after the triple.
④switch statement: This statement is also a multi-branch selection statement. Which block is executed depends on the switch setting, that is, the path where the value of the expression matches the constant expression. It is different from the if…else statement, and all its branches are parallel. When the program is executed, the first branch begins to search, and if it matches, the subsequent blocks are executed, and then the blocks of the second branch and the third branch are executed until. If not, find out whether the next branch matches. When applying this statement, we should pay special attention to the reasonable setting of switch conditions and the reasonable application of break statement.
(3) periodic structure:
Loop structure can reduce the workload of repeated writing in source program and can be used to describe the problem of repeated execution of an algorithm. This is the program structure that can give full play to the computer specialty in programming. C language provides four loops, which are goto loop, while loop, do -while loop and for loop. Four cycles can be used to deal with the same problem. Generally, they can be replaced by each other, but it is generally not recommended to use goto loop, because forcibly changing the order of programs will often bring unpredictable errors to the operation of programs. In study, we mainly study while, do…while and for. The key point of learning three commonly used loop structures is to find out their similarities and differences so as to use them in different situations. This requires a clear understanding of the format and execution order of the three loops, and a thorough understanding of the flow chart of each loop, which will help you understand how to replace them. For example, take the while loop as an example, rewrite a program with a for statement to better understand their functions. Pay special attention to the statements that tend to end in the loop body (that is, the change of loop variable value), otherwise it may become an infinite loop, which is a common mistake for beginners.
After learning these three loops, we should make clear their similarities and differences: when using while and do…while loops, the initialization operation of loop variables should be before the loop body, while the for loop is generally carried out in the statement 1; Both the while loop and the for loop judge the expression before executing the loop body, while the do…while loop judges the expression first, which means that the loop body of the do…while loop is executed at least once, while the while loop and the for loop may not be executed once. In addition, it should be noted that all three kinds of loops can jump out of the loop with break statement and end the loop with continue statement, while the loop formed by goto statement and if cannot be controlled with break and continue statement.
Sequence structure, branching structure and cyclic structure are not isolated from each other. A loop can have branches and sequence structures, and branches can also have loops and sequence structures. In fact, no matter what kind of structure, we can regard them as statements in a broad sense. In the actual programming process, these three structures are often combined with each other to realize various algorithms and design corresponding programs. But the problem of programming is big, and the written programs are often very long and repetitive, which leads to poor readability and difficult 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 C language is realized by functions, that is, the complex C program is divided into several modules, each module is written as a C function, and then a large-scale C program is written by calling functions from the main function and functions from the function, so it is often said that C program = main function+sub-function. Therefore, we should pay special attention to understanding and application in the definition, call and return value of functions, and consolidate them through computer debugging.