First, the class C language mentioned here refers to the same or similar programming languages such as C, C , C # and Java. In these languages, the syntax and execution flow of the for statement are the same. This article will conduct a more in-depth discussion on the usage of this statement.
Users who are familiar with the Basic language know that in the Basic, the FOR cycle is determined by specifying initial values, final values, and steps to determine a loop. For example, to initialize an array of length 10, we should write:
For i = 0 to 9 Step 1
A (i) = i * i
Next I
Where the step size and the variable specified in the NEXT statement can be omitted. Therefore, the loop above can even be written with quite simple.
FOR i = 1 to 10
A (i) = i * i
NEXT
We can see that the readability of this statement is quite high, just want to sweep, we know that this loop will execute 10 times, and initialize each element of an array of 10 to its subscript. value.
But if we want to write this in class C language:
FOR (i = 0; i <= 9; i ) {
a [i] = i * i;
}
For readers who are familiar with Class C, this code may have learned; but for an initiator, it will always feel a bit difficult to make it, especially if it is difficult to determine the time of cyclic termination. For example, compare the following two cycles:
/ * Cycle 1: * /
FOR (i = 0; i <= 9; i ) {
a [i] = i * i;
}
/ * Cycle 2: * /
For (i = 0; i <9; i ) {
a [i] = i * i;
}
Readers who are familiar with Class C are clear that the cycle 1 will perform 10 times and the cyclic 2 will only perform 9 times; and the loop 2 is just a form commonly used by most class C language programmers.
So, everyone may have questions: class C language is well received by the simplicity of their grammar and the efficiency of the execution, why is it complicated in the for loop?
We know that the C language has also achieved a high goal in flexibility while pursuing simple and efficient. This design of the For language is a high degree of flexibility.
First, we talk about the implementation process of the FOR statement of class C language.
The FOR statement of class C language usually has the following form:
FOR (statement1; statement2; statement3) {
/ * body * /
}
Here, Statement1, Statement2, and Statement3 are a general statement. STATEMENT2 should have a Boolean type, but there is an int type in C (because the C language does not support the Boolean type). The execution process of this statement is as follows:
Step 1: Ask Statement1.
Step 2: Judge whether statement2 is true (it is not 0 in c), if it is not true (0 is 0), it will continue; otherwise continue
Step 3: Perform a cyclic body.
Step 4: Perform Statement3 and go to step 2.
Some beginners (including me past) may do so before do not understand the implementation process of the FOR statement, the STATEMENT1 (usually a assignment statement) in the class C language FOR statement is equivalent to the initial value setting in the Basic language for statement. STATEMENT2 statement in the class C language FOR statement (usually a statement of a determination size) is equivalent to the final value judgment in the Basic language for statement, and the statement3 statement in the class C language (usually a self-incoming statement for loop variables) ) The step size settings are equivalent to the Basic language for statement. However, after understanding this process, we should flexibly use the C language for statement. For a FOR cycle, we can first initialize the loop process through Statement1 (through the comma expression in class C language, can even perform multiple statements here), then make judgments on the termination of the cycle in Statement2, while The state of changing the cycle process via statement3 after execution of the secondary cycle.
For example, I developed my own library with the C language when I learned the data structure, where I used the process of finding the insertion point (delete point) position in insertion, deletion, and traversal, etc. The flexibility of the for statement. Here is traversed as an example:
TYPEDEF VOID (* Walk_FUNC_T) (VOID *);
Void Walkon (SLLIST * LIST, WALK_FUNC_T WALK) {
SllistNode * P;
FOR
P = list-> head; / * Setting initial status * /
p! = null; / * Judgment the cycle ends * /
P = P-> Next / * Enter the next state * /
) {
Walk (P-> Data); / * Traversing the current node * /
}
}
Here, I don't want to discuss the specific details of the list, just consider the For loop, the implementation process is as follows:
The first step: set P to point the first node to the linked list.
Step 2: Determine whether the P is empty (the end of the end of the chain), if the loop is exited for the air; otherwise continue.
Step 3: Perform some operations on the data objects pointed to by the transferred function pointer to the current node (node pointed to P).
Step 4: Let P point to the next node; go to the second step.
With such a process, we can clearly see the execution process of traversal action.
Many people see this code may be quite depressed, they usually (even certain) write this function:
Void Waklon (SLLIST * LIST, WALK_FUNC_T WALK) {
SllistNode * p = list-> head;
While (p! = null) {
Walk (P-> DATA);
P = P-> next;
}
}
Everyone may think such code is more readable. But I think this is because everyone does not understand the implementation of various statements of class C language. Since there is no master mentioned my usage, many people are unacceptable. However, when everyone understands the implementation process of the for statement, the method I mentioned is actually more readable. In the code I have written above, a good FOR statement clearly shows the process of traverstive action (the annotation is completely removed); and if you want to understand the traversal function implemented by the While loop, you will ask the code to read the code. I have talented the linked list and its traversal action.