algorithm - Execute a loop x times without loop or if statements -
how can rewrite following program in order not use loop , branch constructs? (no if, while, break, continue, switch, ...)
for(int i=0; < 5; i++){ // stuff }
the approach can think of use ugly goto statements:
loop: // stuff goto loop;
but how can exit loop after 5 runs? or there different way?
edit: solution should not recursive. function calls not yet allowed in course.
you can use recursive function , pass argument counter.
decrease counter each time before calling.
int func(int a,int counter) { int c; // .. logic return counter==0?a:func(a,counter-1); }
this line return counter==0?a:func(a,counter-1);
helps handling condition when counter==0
without using if.