1

How can I rewrite the following program in order to not use any loop and branch constructs? (No if, while, break, continue, switch, ...)

for(int i=0; i < 5; i++){
  // do stuff
}

The only approach I can think of is to use ugly goto statements:

loop:
  // do stuff
  goto loop;

But how can I exit this loop after exactly 5 runs? Or is there a different way?

Edit: The solution should not be recursive. Function calls are not yet allowed in the course.

00FF00FF
  • 31
  • 3
  • 1
    You'll need at least an `if` with your goto to immitate a loop. – AliciaBytes Apr 23 '15 at 07:39
  • yes: `//do stuff //do stuff //do stuff //do stuff //do stuff `. However, the point of it evades me. – amit Apr 23 '15 at 07:40
  • Please explain the usefulness of this... I mean why do you NOT want to use loops and goto? – unrealsoul007 Apr 23 '15 at 07:40
  • To be honest: It is a small part of an assignment. But I doubt this is even possible. The answer of @amit of course works, but in reality I need 32 of them. I don't think this is the desired way. – 00FF00FF Apr 23 '15 at 07:42
  • If this is course work then it is ridiculous. What is this supposed to teach you? Repeat the statement that you want five times. – gnasher729 Apr 23 '15 at 08:24

4 Answers4

1

You can use a recursive function and pass an argument as a counter.
Decrease the counter each time before calling.

int func(int a,int counter)
{
  int c;


  // .. your logic
  return counter==0?a:func(a,counter-1);      

}

This line return counter==0?a:func(a,counter-1); helps you handling the condition when counter==0 without using if.

Srinath Mandava
  • 3,384
  • 2
  • 24
  • 37
0

You could use a foreach loop:

// An array of ints with 5 values
int[] someNumbers = new int[] {0, 1, 2, 3, 4};

// Then foreach through someNumbers
foreach (int aNumber in someNumbers)
{
    // Do Stuff
}

It is a bit of a hack but it will work fine, also if you want make someNumbers public and put it in some secluded space in your code, so it is not clogging your code up :).

Winky2222
  • 46
  • 7
0

I found out this is not possible. Thanks for the answers anyway!

00FF00FF
  • 31
  • 3
0

If you are using GCC, you can use the feature "label as value" (you can also do similar stuff in other compilers), like this:

#include <stdio.h>

int main() {
    int n, v, c;

    static void* labels[] = {&&nope, &&again};

    n = 5;
    again:
        printf("stuff\n");
        n--;
        goto *labels[__builtin_popcount(n&-n)];        
    nope:
        printf("done!\n");
    return 0;
}

This code finds the lowest set bit in n (to reduce it to only one bit, if any) and do a popcount on it. In practice this will return 0 if the n==0 or 1 otherwise. If you don't want to rely on __buitin_popcount, you can implement your own, like this:

int popcount(int v) {
    int c;
    c = (v & 0x55555555) + ((v >> 1) & 0x55555555);
    c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
    c = (c & 0x0F0F0F0F) + ((c >> 4) & 0x0F0F0F0F);
    c = (c & 0x00FF00FF) + ((c >> 8) & 0x00FF00FF);
    c = (c & 0x0000FFFF) + ((c >> 16)& 0x0000FFFF);
    return c;
}
Community
  • 1
  • 1
Juan Lopes
  • 10,143
  • 2
  • 25
  • 44