Hi. This should be very easy for you guys, but I still can't get it visualized in my mind. Here's what I'm trying to do:

if (flag)
    for (i = 0; i < 10; i++)
        {LARGE_BLOCK_OF_CODE (that visits an array in order)}
else
    for (i = 9; i >= 0; i--)
        {LARGE_BLOCK_OF_CODE (that visits an array in REVERSE order)}

The problem is that this block of code cannot be placed in a function, and I don't want to copy/paste it twice. Is there any way I can do that in this manner:

if (flag)
    loop = for (i = 0; i < 10; i++);
else
    for (i = 9; i >= 0; i--);

loop
    {LARGE_BLOCK_OF_CODE (that visits an array in the order specified in 'loop')}

??
Thanks.

Recommended Answers

All 5 Replies

The problem is that this block of code cannot be placed in a function, and I don't want to copy/paste it twice.

Maybe I'm not reading that right, but what is your requirement for this block?

And no, you can't assign a for loop to a variable.

They haven't told me what's in that block of code, but all I know is that it's got lists, arrays, iterators, and similar stuff. I can ask them for the exact code if the specifics will make a difference. But AFAIK, they need something generic that can work on several blocks.

No, the specifics aren't important, I think I see more of what you're driving at now. Again, not anything that I could come up with (short of a giant #define, but I'm not so sure that would even work). See what other people think...

OK, here's a trick. The hard part, as you will see, is figuring out how to unify the comparisons--i.e., to make the comparisons the same for each loop. So let's start in that direction by changing the comparison to inequality comparisons:

if (flag)
    for (i = 0; i != 10; i++)
        {LARGE_BLOCK_OF_CODE (that visits an array in order)}
else
    for (i = 9; i != -1; i--)
        {LARGE_BLOCK_OF_CODE (that visits an array in REVERSE order)}

Now let's unify the increment/decrement statements:

if (flag)
    for (i = 0; i != 10; i += 1)
        {LARGE_BLOCK_OF_CODE (that visits an array in order)}
else
    for (i = 9; i != -1; i += -1)
        {LARGE_BLOCK_OF_CODE (that visits an array in REVERSE order)}

Now the form of our two for statements is identical except for the values of three constants. So we can unify them by making those constants into variables:

int start, end, direction;

if (flag) {
    start = 0;
    end = 10;
    direction = 1;
} else {
    start = 9;
    end = -1;
    direction = -1;
}

for (i = start; i != end; i += direction)
    {LARGE_BLOCK_OF_CODE (that visits an array in the specified order)}
commented: Good one +5
commented: Not only gave the answer, but explained it really well. Thank you! +1

@jonsca:

I just looked at the code, and it ain't pretty. I also don't understand anything from it; all I look is a bunch of iterators and stuff. I don't think I want to move it to a separate function, since it's someone else's code. Thanks for your help.

@arkoenig:
Looks promising. Very well explained. Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.