2

is that possible to make power of loop depending on some variable. I am not sure if "power of loop" is suitable term, but can't find better. I mean for example something like:

int powerOfLoop = 8;

for(int a0=0; a0<something0; a0++)
{
    for(int a1=0; a1<something1; a1++)
    {
        for(int a2=0; a2<something2; a2++)
        {
            for(int a3=0; a3<something3; a3++)
            {
                // .... and so on until "a8"
            }
        }
    }
}

And If I change powerOfLoop then power (or depth) of that loop also change. Is that possible at all? How to implement it easly?

pajczur
  • 235
  • 3
  • 10
  • Why not use recursion? – UnholySheep Apr 08 '18 at 21:50
  • What do you mean? What is recursion? Of course I go to google now, but if you could explain it in regards to my question it would be great? – pajczur Apr 08 '18 at 21:52
  • @pajczur This is the first time I've seen someone walk into one of Google's easter eggs without prior knowledge :P. – HTNW Apr 08 '18 at 21:53
  • Aha, you mean to make function and inside of it call that function? – pajczur Apr 08 '18 at 21:53
  • As @UnholySheep said, you are looking for a recursive algorithm. – mfnx Apr 08 '18 at 21:54
  • 1
    There's no such thing as "power of loop" in C++. As others have mentioned, you're probably looking for a recursive algorithm. This is not something that can be explained in one or two paragraphs on stackoverflow.com, but should be fully explained in your C++ book. – Sam Varshavchik Apr 08 '18 at 21:57
  • It's good to see simple beginner questions, and to see people help :) @pajczur recursion is what you're looking for; good luck. – NonCreature0714 Apr 08 '18 at 21:57
  • OK, thanks for all answers, I've already found the solution in C++ documentation :) – pajczur Apr 08 '18 at 21:58
  • Just didn't know the word "recursion" :) Now I know – pajczur Apr 08 '18 at 21:59
  • Is the "power" of the loop a constant known at compile time or is it a runtime value? The linked duplicate only addresses the later, and wouldn't it be a shame to let such an opportunity for some template meta programming pass? – Daniel Jour Apr 08 '18 at 22:41

0 Answers0