-2

Basically I have got four functions

f1
f2
f3 
f4

and I need to create a for loop or while loop that will call each function for each iteration

so for first iteration

1st = f1

for second

2nd = f2

if the loop were to run for 8 times it would be

f1()
f2()
f3()
f4()
f1()
f2()
f3()
f4()

I am using java and I think that I will need to use the % operator

Thanks

bighead
  • 135
  • 1
  • 1
  • 10

2 Answers2

1

The easiest way would be to use a switch and the modulus operator on the loop index. For example:

public class Main {

    static void f1() {
        System.out.println("f1");
    }
    static void f2() {
        System.out.println("f2");
    }
    static void f3() {
        System.out.println("f3");
    }
    static void f4() {
        System.out.println("f4");
    }

    public static void main(String[] args) {

        for (int i = 0; i < 16; ++i) {
            switch (i % 4) {
                case 0:
                    f1();
                    break;
                case 1:
                    f2();
                    break;
                case 2:
                    f3();
                    break;
                case 3:
                    f4();
                    break;
            }
        }
    }
}

As @Andreas points out, once you remember the right type to use for the function (Runnable in this case), this can be done with an array:

    // ... 
    static Runnable[] functions = 
        { Main::f1, Main::f2, Main::f3, Main::f4 };

    public static void main(String[] args) {

        for (int i = 0; i < 16; ++i)
            functions[i % 4].run();

    }

Runnable isn't really consistent with Callable so this seems like a bit of a mis-use, but I don't see a simple way to do this via types in java.util.function.

sfjac
  • 7,119
  • 5
  • 45
  • 69
  • Thanks the course I am doing never mentioned switches so Ill need to look up on it – bighead Dec 07 '16 at 17:24
  • I removed a comment that you can't do method references. I know I can get the reference to such a function (`Main::f1` etc.), but the api for using `Function` is, in this case, more painful than doing an interface. – sfjac Dec 07 '16 at 17:41
  • @Andreas Remembering that Runnable is the type of a function taking no arguments and returning void! :) Also, that seems like a mis-use of the type, which is meant for threading. – sfjac Dec 07 '16 at 17:48
  • @sfjac See "[functional interface that takes nothing and returns nothing](http://stackoverflow.com/q/23958814/5221149)". – Andreas Dec 07 '16 at 18:02
  • @Andreas Glad to know I'm not the only one who finds this puzzling. :) – sfjac Dec 07 '16 at 18:05
  • I flagged the comment as obsolete - perhaps that comment thread should be removed to avoid confusion? – sfjac Dec 07 '16 at 18:06
-2

nested if-else of switch can do the job. Ranging, 1 to 4 , as you have 4 functions. I don't think you will need % operator to do so.

hasham.98
  • 73
  • 3
  • 1
    you need a `%` operator (or equivalent logic to reset the function dispatching counter to 1) if the loop can run for more than four iterations – Alnitak Dec 07 '16 at 17:19