Implementing an Incremental Loop Structure in C Language

Content

Hello everyone, I am Bug Jun~

Today is the Qixi Festival, and the internet is filled with jokes, with various videos of flowers and takeout appearing everywhere. You might think it’s just for a laugh after meals, but it quietly influences some people, as everyone enjoys daydreaming~

Alright, today Bug Jun will talk about technology to cleanse the soul.

Recently, in a project, I needed to control multiple devices that have some interrelations, requiring me to access these devices in an incremental looping manner. For example:

First loop iteration

0~9

Second loop iteration

1~9,0
Third loop iteration 2~9,0~1

And so on~

1

Initial Approach

The first and most straightforward method is this step-by-step approach:

#include <stdio.h>

int main() {
    for (int start = 0; start < 100; start++) {
        // From the current starting value to 99
        for (int i = start; i < 100; i++) {
            printf("%d ", i);//do something1
        }
        // From 0 to the starting value
        for (int j = 0; j < start; j++) {
            printf("%d ", j);//do something2
        }
        printf("\n");
    }
    return 0;
}

This method is intuitive and easy to understand; it first loops out and then loops back, resulting in many iterations and a lot of code, with the code scattered across two locations. If we don’t encapsulate the ‘do something’ in a function, the redundant code can be quite extensive~

2

Optimized Approach

To make the code shorter, simpler, and still readable, we can further optimize this structure using the modulus operation:

#include <stdio.h>

int main() {
    for (int start = 0; start < 100; start++) {
        for (int i = start; i < start + 100; i++) {
            printf("%d ", i % 100 );//do something
        }
        printf("\n");
    }
    return 0;
}

Using a single modulus operator replaces a lot of code~

Of course, there is another issue worth discussing among embedded engineers, which is efficiency. The modulus operation is essentially a division operation, and division is an expensive operation in the CPU (it may take 10-40 clock cycles), while the traditional approach only uses simple addition and comparison.

Therefore, there is no absolute superiority; if there were an absolute advantage, it would imply replacement and abandonment.

If performance is a priority, on performance-critical paths, it is recommended to use the traditional nested loops, especially at high optimization levels where the performance advantage is more evident; if performance does not need to be so stringent, the modulus approach results in more concise and compact code.

In fact, such requirements often arise in embedded systems, such as:

  1. Circular buffer processing
  2. Polling scheduling algorithms

Perhaps the charm of programming lies in the fact that through clever mathematical thinking and algorithm design, we can achieve complex functionalities with extremely concise code.

Finally

That’s all for today. If you found this helpful, be sure to give alike~

A unique, permanent, and free platform for sharing embedded technology knowledge~

Recommended Collections Click the blue text to jump

MCU Advanced Collection Implementing an Incremental Loop Structure in C Language

Embedded C Language Advanced Collection Implementing an Incremental Loop Structure in C Language

“Bug Says” Collection Implementing an Incremental Loop Structure in C Language

Collection | Comprehensive Programming for Linux Applications

Collection | Learn Some Networking Knowledge

Collection | Handwritten C Language

Collection | Handwritten C++ Language

Collection | Experience Sharing

Collection | Power Control Technology

Collection | From Microcontroller to Linux

Leave a Comment