What is the most elegant way to loop TWICE in C

Many times I need to do things TWICE in a for loop. Simply I can set up a for loop with an iterator and go through it twice:

for (i = 0; i < 2; i++)
 // Do stuff

Now I am interested in doing this as SIMPLY as I can, perhaps without an initializer or iterator? Are there any other, really simple and elegant, ways of achieving this?


What about this??

void DostuffFunction(){}

for (unsigned i = 0; i < 2; ++i, DostuffFunction());

Regards, Pablo.

This is elegant because it looks like a triangle; and triangles are elegant.

i = 0; 
here: dostuff(); 
i++; if ( i == 1 ) goto here;

Encapsulate it in a function and call it twice.

void do_stuff() {
  // Do Stuff

// .....


Note: if you use variables or parameters of the enclosing function in the stuff logic, you can pass them as arguments to the extracted do_stuff function.

If its only twice, and you want to avoid a loop, just write the darn thing twice.

statement1;  // (again)

If the loop is too verbose for you, you can also define an alias for it:

#define TWICE for (int _index = 0; _index < 2; _index++)

This would result into that code:

    // Do Stuff

// or


I would only recommend to use this macro if you have to do this very often, I think else the plain for-loop is more readable.

Unfortunately, this is not for C, but for C++ only, but does exactly what you want:

Just include the header, and you can write something like this:

10 times {
  // Do stuff

I'll try to rewrite it for C as well.

So, after some time, here's an approach that enables you to write the following in pure C:

2 times {

You'll have to include this little thing as a simple header file (I always called the file extension.h). Then, you'll be able to write programs in the style of:


int main(int argc, char** argv){
    3 times printf("Hello.\n");
    3 times printf("Score: 0 : %d\n", _);
    2 times {
        printf("Counting: ");
        9 times printf("%d ", _);
    5 times {
        printf("Counting up to %d: ", _);
        _ times printf("%d ", _);
    return 0;
  • Simple notation of simple loops (in the style depicted above)
  • Counter is implicitly stored in a variable called _ (a simple underscore).
  • Nesting of loops allowed.
Restrictions (and how to (partially) circumvent them):
  • Works only for a certain number of loops (which is - "of course" - reasonable, since you only would want to use such a thing for "small" loops). Current implementation supports a maximum of 18 iterations (higher values result in undefined behaviour). Can be adjusted in header file by changing the size of array _A.
  • Only a certain nesting depth is allowed. Current implementation supports a nesting depth of 10. Can be adjusted by redefining the macro _Y.

You can see the full (=de-obfuscated) source-code here. Let's say we want to allow up to 18 loops.

  • Retrieving upper iteration bound: The basic idea is to have an array of chars that are initially all set to 0 (this is the array counterarray). If we issue a call to e.g. 2 times {do_it;}, the macro times shall set the second element of counterarray to 1 (i.e. counterarray[2] = 1). In C, it is possible to swap index and array name in such an assignment, so we can write 2[counterarray] = 1 to acchieve the same. This is exactly what the macro times does as first step. Then, we can later scan the array counterarray until we find an element that is not 0, but 1. The corresponding index is then the upper iteration bound. It is stored in variable searcher. Since we want to support nesting, we have to store the upper bound for each nesting depth separately, this is done by searchermax[depth]=searcher+1.
  • Adjusting current nesting depth: As said, we want to support nesting of loops, so we have to keep track of the current nesting depth (done in the variable depth). We increment it by one if we start such a loop.
  • The actual counter variable: We have a "variable" called _ that implicitly gets assigned the current counter. In fact, we store one counter for each nesting depth (all stored in the array counter. Then, _ is just another macro that retrieves the proper counter for the current nesting depth from this array.
  • The actual for loop: We take the for loop into parts:
    • We initialize the counter for the current nesting depth to 0 (done by counter[depth] = 0).
    • The iteration step is the most complicated part: We have to check if the loop at the current nesting depth has reached its end. If so, we have do update the nesting depth accordingly. If not, we have to increment the current nesting depth's counter by 1. The variable lastloop is 1 if this is the last iteration, otherwise 0, and we adjust the current nesting depth accordingly. The main problem here is that we have to write this as a sequence of expressions, all separated by commata, which requires us to write all these conditions in a very non-straight-forward way.
    • The "increment step" of the for loop consists of only one assignment, that increments the appropriate counter (i.e. the element of counter of the proper nesting depth) and assigns this value to our "counter variable" _.

What abelenky said.

And if your { // Do stuff } is multi-line, make it a function, and call that function -- twice.

Another attempt:

for(i=2;i--;) /* Do stuff */

This solution has many benefits:

  • Shortest form possible, I claim (13 chars)
  • Still, readable
  • Includes initialization
  • The amount of repeats ("2") is visible in the code
  • Can be used as a toggle (1 or 0) inside the body e.g. for alternation
  • Works with single instruction, instruction body or function call
  • Flexible (doesn't have to be used only for "doing twice")
  • Dijkstra compliant ;-)

From comment:

for (i=2; i--; "Do stuff");

Use function:


Or use macro (not recommended):

#define DO_IT_TWICE(A) A; A

DO_IT_TWICE({ x+=cos(123); func(x); })

If your compiler supports this just put the declaration inside the for statement:

for (unsigned i = 0; i < 2; ++i)
 // Do stuff

This is as elegant and efficient as it can be. Modern compilers can do loop unrolling and all that stuff, trust them. If you don't trust them, check the assembler.

And it has one little advantage to all other solutions, for everybody it just reads, "do it twice".

Many people suggest writing out the code twice, which is fine if the code is short. There is, however, a size of code block which would be awkward to copy but is not large enough to merit its own function (especially if that function would need an excessive number of parameters). My own normal idiom to run a loop 'n' times is

  i = number_of_reps;
    ... whatever
  } while(--i);

In some measure because I'm frequently coding for an embedded system where the up-counting loop is often inefficient enough to matter, and in some measure because it's easy to see the number of repetitions. Running things twice is a bit awkward because the most efficient coding on my target system

  bit rep_flag;

  rep_flag = 0;
  } while(rep_flag ^= 1); /* Note: if loop runs to completion, leaves rep_flag clear */

doesn't read terribly well. Using a numeric counter suggests the number of reps can be varied arbitrarily, which in many instances won't be the case. Still, a numeric counter is probably the best bet.

Assuming C++0x lambda support:

template <typename T> void twice(T t)

twice([](){ /*insert code here*/ });


    /*insert code here*/ 

Which doesn't help you since you wanted it for C.

As Edsger W. Dijkstra himself put it : "two or more, use a for". No need to be any simpler.

Good rule: three or more, do a for.

I think I read that in Code Complete, but I could be wrong. So in your case you don't need a for loop.

This is the shortest possible without preprocessor/template/duplication tricks:

for(int i=2; i--; ) /*do stuff*/;

Note that the decrement happens once right at the beginning, which is why this will loop precisely twice with the indices 1 and 0 as requested.

Alternatively you can write

for(int i=2; i--; /*do stuff*/) ;

But that's purely a difference of taste.

If what you are doing is somewhat complicated wrap it in a function and call that function twice? (This depends on how many local variables your do stuff code relies on).

You could do something like

void do_stuff(int i){
    // do stuff


But this may get extremely ugly if you are working on a whole bunch of local variables.

//dostuff (Attention I am doing the same stuff for the :**2nd** time)

First, use a comment

/* Do the following stuff twice */

then, 1) use the for loop 2) write the statement twice, or 3) write a function and call the function twice do not use macros, as earlier stated, macros are evil.

(My answer's almost a triangle)

What is elegance? How do you measure it? Is someone paying you to be elegant? If so how do they determine the dollar-to-elegance conversion?

When I ask myself, "how should this be written," I consider the priorities of the person paying me. If I'm being paid to write fast code, control-c, control-v, done. If I'm being paid to write code fast, well.. same thing. If I'm being paid to write code that occupies the smallest amount of space on the screen, I short the stock of my employer.

jump instruction is pretty slow,so if you write the lines one after the other,it would work faster,than writing a loop. but modern compilers are very,very smart and the optimizations are great (if they are allowed,of course). if you have turned on your compiler's optimizations,you don't care the way,you write it - with loop or not (:

EDIT : http://en.wikipedia.org/wiki/compiler_optimizations just take a look (:

Close to your example, elegant and efficient:

for (i = 2; i; --i)
    /* Do stuff */

Here's why I'd recommend that approach:

  • It initializes the iterator to the number of iterations, which makes intuitive sense.
  • It uses decrement over increment so that the loop test expression is a comparison to zero (the "i;" can be interpreted as "is i true?" which in C means "is i non-zero"), which may optimize better on certain architectures.
  • It uses pre-decrement as opposed to post-decrement in the counting expression for the same reason (may optimize better).
  • It uses a for loop instead of do/while or goto or XOR or switch or macro or any other trick approach because readability and maintainability are more elegant and important than clever hacks.
  • It doesn't require you to duplicate the code for "Do stuff" so that you can avoid a loop. Duplicated code is an abomination and a maintenance nightmare.

If "Do stuff" is lengthy, move it into a function and give the compiler permission to inline it if beneficial. Then call the function from within the for loop.

void loopTwice (bool first = true)
    // Recursion is your friend
    if (first) {loopTwice(false);}

    // Do Stuff

I'm sure there's a more elegant way, but this is simple to read, and pretty simply to write. There might even be a way to eliminate the bool parameter, but this is what I came up with in 20 seconds.

Need Your Help

How to split a string every 4 chars and then memorize the fragments

c string split

Does anybody know how to split a string every (e.g.) 4 chars?

Having file in hive wareshouse

hadoop hive

I have a file sample.txt and i want to place it in hive warehouse directory (Not under the database xyz.db but directly into immediate subdirectory of warehouse). Is it possible?