Usage of for statements in C Language

Source: Internet
Author: User
First, the class C language mentioned here refers to the same or similar programming languages as C, C ++, C #, and Java. In these languages, the syntax and execution process of the for statement are the same. This article will discuss the usage of this statement in depth.
Users familiar with the BASIC language know that in BASIC, a for loop is determined by specifying the initial value, the final value, and the step size. For example, to initialize an array with a length of 10, we should write:
FOR I = 0 TO 9 STEP 1
A (I) = I * I
NEXT I
The step size and the variables specified in the NEXT statement can be omitted. Therefore, the above loop can even be written quite simply:
FOR I = 1 TO 10
A (I) = I * I
NEXT
We can see that the readability of this statement is quite high. Just scan it and we will know that this loop will be executed 10 times, and initialize each element of an array with a length of 10 to its square value.
However, if we want to write in C language like this:
For (I = 0; I <= 9; I ++ ){
A [I] = I * I;
}
For readers familiar with C language, this piece of code may have been used to; but for a beginner, it will always feel a bit unpredictable, especially it is difficult to judge the time when the cycle ends. For example, compare the following two cycles:
/* Loop 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 C language know that loop 1 will execute 10 times, while loop 2 will only execute 9 times. Loop 2 is exactly a common form of C language programmers.
So, you may be wondering: C-like languages are very popular with their concise syntax and high execution efficiency. Why is it so complicated in the for loop?
We know that C language is pursuing simplicity and efficiency while achieving a high goal in flexibility. This design of for language is a reflection of high flexibility.
First, let's talk about the execution process of for statements in C language.
The for statement in C language generally has the following forms:
For (statement1; statement2; statement3 ){
/* Body */
}
Here, statement1, statement2, and statement3 are general statements. Among them, statement2 should have the boolean type, but it has the 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: Evaluate statement1.
Step 1: judge whether statement2 is true (whether it is not 0 in C). If it is not true (0 in C), the execution ends; otherwise, the execution continues.
Step 2: Execute the loop body.
Step 3: Execute statement3 and go to step 3.
Before learning about the execution process of the for statement, some beginners (including me in the past) may think that statement1 (usually a value assignment statement) in a C-like for statement) it is equivalent to the initial value setting in the FOR statement of the BASIC language. The statement2 Statement (usually a statement to determine the size) in the for statement of the C language is equivalent to the final value judgment in the FOR statement of the BASIC language, the statement3 statement in C Language (usually a self-added statement FOR loop variables) is equivalent to the step size setting in the FOR statement in BASIC language.
However, after learning about this process, we should use the for statement in C Language flexibly. For a for loop, We can first initialize the loop process through statement1 (using a comma expression in C language, or even executing multiple statements here ), then, statement2 judges the termination of the loop, and changes the state of the loop process through statement3 after each loop body is executed.
For example, I developed my own function library in C language when learning the data structure. In this function, I used operations such as inserting, deleting, and traversing the linked list to find the insertion point (delete point) in the process of location, I used a lot of flexible usage of for statements. The following uses traversal as an example:
Typedef void (* pai_func_t) (void *);

Void login on (SLList * list, pai_func_t walk ){
SLListNode * p;

For (
P = list-> head;/* set the initial state */
P! = NULL;/* determine when the loop ends */
P = p-> next/* enter the next state */
){
Walk (p-> data);/* traverse the current node */
}
}
Here, I don't want to discuss the details of the linked list. I only consider the for loop. the execution process is as follows:
Step 1: Set p to point to the first node of the linked list.
Step 2: Determine whether p is null (to the end of the linked list). If it is null, exit the loop; otherwise, continue.
Step 3: Use the passed function pointer to execute some operations on the Data Object pointed to by the current node (the node pointed to by p.
Step 4: point p to the next node and go to step 2.
Through this process, we can clearly see the execution process of traversal actions.
Many people may find this Code quite depressing. They usually (or even some) write this function as follows:
Void waklon (sllist * List, pai_func_t walk ){
Sllistnode * P = List-> head;

While (P! = NULL ){
Walk (p-> data );
P = p-> next;
}
}
You may think this code is more readable. However, I think this is because you do not know much about the execution of various statements in C language. Since no master mentioned my usage, it is unacceptable for many people. However, when you have some knowledge about the for statement execution process, the method I mentioned is actually more readable. In the code I wrote above, a well-formatted for statement clearly shows the process of traversing the action (annotations can be completely removed ); to understand the traversal function implemented by the while loop, users who read the code need to understand the linked list and its traversal actions.
Finally, this article compares basic and C language to demonstrate the syntax flexibility of C language. Then I introduced the usage of a for statement I invented myself, and made full use of this flexibility to greatly improve the readability of the program. No matter whether you like my method or whether you can understand it, I hope everyone should learn more about the programming language itself, which is the basis for compiling utilities. We should make full use of language flexibility to design programs with higher readability.

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.