-1

I have a the given for loop written multiple times and it is annoying to type that everytime.

Note that they are in they are in different functions.so, code optimization isn't the problem here. but instead representation

for(int j=0; j<PS; j++){
   for (int k=0; k<CLS;k++){

Would it be possible to represent it with the help of a pragma or something.. so, that I can use it in the following fashion..

#pragma mycurrentlooping
{
  blah[j][k]...
}

Would this be possible? if yes, how? or what is the best possible way to do this.

Would doing this effect the performance?

This may sound stupid..but i really have many such individual functions that do different operations.

user3808088
  • 87
  • 1
  • 1
  • 10
  • 1
    -1: I think it is bad idea. If a partner has to read your code, it would be very "annoying" for him, because (s)he had to descipher your exotic syntax every time. The standard syntax of C for loops is well understood by every programmer. People not only write programs, but read them, also, and extrange code means a waste of time. – pablo1977 Jul 18 '14 at 10:00

2 Answers2

3

You can use #define to define a MACRO for that, like this:

#define loop_till(x) for(int j=0; j<(x); j++)

You do it at the top of your source file, then use it as:

loop_till(PS)
{
     loop_till(CLS)
     {
      ...
     }
}

To read more about (function like) pre-processor macros, read this

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • 2
    Add a parameter for the counter name and it's solved. – Quentin Jul 18 '14 at 09:43
  • But can it be a two level for loop? Can you show me an example? – user3808088 Jul 18 '14 at 09:43
  • 5
    I don't like this approach. The index variables j, k, are used in the block tied to the for(;;) loop, but their declaration is hidden to the programmer by the macro. So, the answer is correct, but I think that is prefirable to write the "annoying" code every time, since is even more annoying to have exotic C code over there. – pablo1977 Jul 18 '14 at 09:49
  • When would you use it, Pablo1977? – user3808088 Jul 18 '14 at 09:57
  • 1
    @pablo1977 :You are right, in that case we can pass `i` or `j` as parameter to macro. But since `i` and `j` are lvalues, I do not like that approach either. – 0xF1 Jul 18 '14 at 10:00
  • @user3808088: What do you mean? – pablo1977 Jul 18 '14 at 10:02
  • @user3808088: Ok, anyway, it seems you have not practice with macros. So, it is a good idea that you learn about that. I like macros a lot, they are really fun, but I avoid to use them most of the time. Macros are very good to define constants, and I use them a lot in debugging stage. – pablo1977 Jul 18 '14 at 10:08
  • I use macros extensively. did not know how to use them for `for loops` and if they were any good. :) – user3808088 Jul 18 '14 at 10:15
1

@Don'tYouWorryChild gives you the correct answer,

But can it be a two level for loop? Can you show me an example?

If you want a one line macro:

#include <stdio.h>

#define LOOP_outer(var, val) for (var = 0; var < val; var++)
#define LOOP_inner LOOP_outer
#define LOOP(outer, inner) LOOP_##outer LOOP_##inner

int main(void)
{
    int i, j;

    LOOP(outer(i, 5), inner(j, 10)) {
        printf("i = %d, j = %d\n", i, j);
    }
    return 0;
}

expands to:

 for (i = 0; i < 5; i++) for (j = 0; j < 10; j++) {
  printf("i = %d, j = %d\n", i, j);
 }

But is bad code (as there is no way to break or continue from the first loop)

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    The "from"-"to" thing is probably wrong, because the real meaning is actually "outer"-"inner". – pablo1977 Jul 18 '14 at 10:31
  • Looks overcomplicated; what's wrong with: `#define LOOP(var, val) for (var = 0; var < val; var++)` followed by `LOOP(i, 5) LOOP(j, 10) { ... }`? – Ruud Helderman Jul 18 '14 at 10:37
  • @Ruud, you are right, and as I say is bad code, but seems that OP wants to call the macro only once. – David Ranieri Jul 18 '14 at 10:42
  • @AlterMann My guess is, OP just wants something short and simple. Oh, and your solution is not necessarily bad code; some reserve is appropriate (risk of obfuscation), but not for the reasons you gave. Too often, `break` and `continue` themselves are a sign of code smell. Oops sorry, didn't mean to start a flame war... – Ruud Helderman Jul 18 '14 at 17:24