C Language Advancement - Analysis of Circular Sentences Defects

Posted by Sharkadder on Tue, 28 Dec 2021 23:54:35 +0100

In the environment of learning materials full of sky, knowledge becomes very scattered and systematic knowledge is not much, which leads many people to work hard every day to move themselves, but ultimately have little effect, or even give up learning. My mission is to filter out a lot of invalid information, to systematize knowledge, to reach the essence of the problem in a short and smooth way, and to free everyone from the pain of finding a needle in a haystack.


Have you had any of the following experiences?

Why do you like to use for loops when you first start learning C, but gradually find that experienced engineers are using while and do-while loops (which should be noticed by those who have looked at the kernel code)?

When writing a loop statement at the beginning, it is always unclear when to use for, when to use while, and when to use do-while. I just got these problems clear, I feel I have mastered all the use of circular statements, but now and then I encounter a new problem - I always have to add code in the circular statements, even before and after the circular statements, to adjust the order of statements, otherwise the final logic will be incorrect, even pointer dereferencing error.

When you encounter these problems, have you ever suspected that looping statements in C language are self-defective? Let's find out what's bothering us with programming ~~

1 Three elements of a loop statement

The three elements of implementing a loop statement are:

  • A. Judgment: Determine if the exit conditions are met or exit the cycle if they are met; If not, the loop continues.
  • B. Execution: Execution of content requiring a loop; Loop variables are typically referenced in content, otherwise loops are indifferent.
  • C. Move: Move the loop variable, otherwise the exit condition will never be satisfied, and the loop will become a dead loop, which is easy for beginners to fall.

Based on the execution order of these three elements, the following six permutations and combinations can be obtained.

numberorder
1A -> B -> C
2A -> C -> B
3B -> C -> A
4C -> B -> A
5B -> A -> C
6C -> A -> B

These six permutations and combinations cannot be avoided in any program you write. Let's analyze them one by one below.

2 Use different loop statements to achieve six permutations and combinations

2.1 First permutation (ABC)

This sequence is the one we are most familiar with and most common with. It is not complicated to implement and is the best case for C-language looping statements.

  1. Use for statement to implement
/* Use for statement to implement ABC */
for (; A; C) {
    B;
}
  1. Using a while statement to implement
/* Implement ABC using the while statement */
while (A) {
    B;
    C;
}

2.2 Second Arrangement (ACB)

We don't always see this order, so the for statement isn't so friendly to support it, but the while statement is unaffected.

  1. Use for statement to implement
/* Use for statement to implement ACB */
for (; A; ;) {
    C;
    B;
}
  1. Using a while statement to implement
/* Implement ACB using the while statement */
while (A) {
    C;
    B;
}

2.3 Third permutation (BCA)

For this kind of arrangement, the for statement has lost its temper completely, and the while statement has shrunk. The do-while statement can show itself.

  1. Use for statement to implement
/* Using for statement to implement BCA */
for (B, C; A; C;) { /*Poor readability and lots of duplicate code*/
    B;
}
  1. Using a while statement to implement
/* Using the while statement to implement BCA */
B; /*You already need to add extra duplicate code outside of the while statement*/
C;
while (A) {
    B;
    C;
}
  1. Using do-while statement to implement
/* Implement BCA using do-while statement */
do {
    B;
    C;
}while (A); /*do-while That's why they were born (it seems that the older generation who designed the sentence carefully considered the arrangement)*/

2.4 Fourth permutation (CBA)

This arrangement follows a similar pattern. But it's even less friendly to the for statement.

  1. Use for statement to implement
/* Use for statement to implement CBA */
for (C, B; A; C,B;) /*It looks like the process is over in one line, but the truth is that B has a lot of content*/
  1. Using a while statement to implement
/* Implement CBA using the while statement */
C; /*Duplicate code*/
B;
while (A) {
    C;
    B;
}
  1. Using do-while statement to implement
/* Implement CBA using do-while statement */
do {
    C;
    B;
}while (A); /*do-while The sentence said silently, "This is my battlefield."*/

2.5 Fifth Arrangement (BAC)

For this arrangement, the cyclic sentences in C think to themselves, what a ghost place this is, I have not seen it before ~~

  1. Use for statement to implement
/* Use for statement to implement BAC */
for (B; A; ;) { /*There are duplicate codes*/
    C;
    B;
}
  1. Using a while statement to implement
/* Using the while statement to implement BAC */
B; /*There are duplicate codes*/
while(A) {
    C;
    B;
}
  1. Using do-while statement to implement

It says it's not at home. Let's go find break.

  1. Using while-break statements
/* Using while-break statements */
while (1) {
 B;
 if (!A)
     break;
 C;
}

2.6 Sixth Arrangement (CAB)

This arrangement is similar to the fifth one, and code duplication is still not resolved without break.

  1. Use for statement to implement
/* Use for statement to implement CAB */
for (C; A; C) {
    B;
}
  1. Using a while statement to implement
/* Using a while statement to implement a CAB */
C;
while (A) {
    B;
    C;
}
  1. Using while-break statements
/* Using while-break statement to implement CAB */
while (1) {
    C;
    if (!A)
        break;
    B;
}

3When to use the for loop statement

By experimenting and exploring above, we find that the best scenario for a for statement is the first arrangement (ABC) scenario, which either degenerates to while or is at a loss when applying to other scenarios.

This is the flaw of the for statement.

So when you look at the Linux kernel or other large projects, there are very few for statements, and what you see basically grows into the following two.

/* for The first application paradigm of loops */
#define MAX 100
int i;
for (i = 0; i < MAX; ++i) {
    /* do somthing you like. */
}

I'm a dividing line.

/* for Second application paradigm for loops */
for (;;) {
    /* do somthing you like. */
}

The second application paradigm is generally used in thread dead loops, of course, while(1) can also be used in dead loops, it can only be said that everyone is so used to it, you can choose the way you like.

4When to use the while loop statement

The while statement applies to the first permutation (ABC) and second permutation (ACB) scenarios.

The feature of this scenario is that A is at the beginning of execution.

5When to use do-while loop statements

The do-while statement applies to the third arrangement (BCA) and fourth arrangement (CBA) scenarios.

The feature of this scenario is that A is executed at the end.

6 Other cases

Other scenarios include the fifth arrangement (BAC) and sixth arrangement (CAB) scenarios, as well as other more complex scenarios.

The feature of this scenario is that A is executed in the middle.

This scenario typically uses the whie-break statement to cover. In fact, it can cover all the scenes.

Students who like shaving roots may ask, how can there be such a scene? Do you really need it?

Let me give you a small example of a real CABC scenario to help you get started.

/* Focus on outer circulation; Focus on pstr pointer variables */
while(1){
    pstr = find_cmd(format_str, pstr); /*C[Move]: This moves the pstr pointer*/
    if (NULL != pstr) /*A[Judgment]: need to be judged before use; See if the pointer is legal or exit the loop if it is illegal*/
    {   
        /* B[Execution]: Start of execution (read string content and print)*/
        printf("----------------------------------------------------------------------------\n");
        printf("|| name    \t| %s \n",pstr->name);
        printf("|| brief   \t| %s \n",pstr->brief);
        for (i = 0; i < pstr->argc; i++)
        {
            if (pstr->argv[i])
                printf("|| @ para%d\t| @ %s \n", i, pstr->argv[i]);
        }
        printf("----------------------------------------------------------------------------\n");
		/* B[Execution]: End of Execution */
        
        pstr++; /*C[Move]: This step or move is a complex scene, an extension of six arrangements.*/
    } else {
        breakļ¼›
    }
}

7 Summary

The article has been a long one, so at the end of the story, I just go to the table.

numberorderAppropriate Loop Statement
1A -> B -> Cfor/while
2A -> C -> Bwhile
3B -> C -> Ado-while
4C -> B -> Ado-while
5B -> A -> Cwhile-break
6C -> A -> Bwhile-break
xComposite Scenewhile-break

Congratulations on continuing to read another blog and making a little progress! If it feels good, let's go. Your comments and concerns will be the driving force for my continued output ~~

Topics: C