3

The variable a can be 0, 1 or 2, where the value is the number of extra while loop conditions to have. I can do this using switch and case but I'm wondering if there's a better way of doing it?

switch (a) {
    case 0: while (condition_1) {
        // ...
    } break;
    case 1: while (condition_1 || condition_2) {
        // ...
    } break;
    case 2: while (condition_1 || condition_2 || condition_3) {
        // ...
    } break;
}

The solution to this problem in Python was to use a dictionary and store the appropriate lambda expressions in the appropriate index. However, my conditions are "not final or effectively final" so they cannot be used in a lambda expression in Java.

The aim here isn't to get either a while (true) or a while (false) and be done with it. It's to start off with a while (...something) which evaluates to false THEN inside the loop do something that changes all of the conditions to true one by one. But until all of the conditions are true, keep looping.

Pseudo code (I know it has some flaws, just for demonstration):

a can be 0,1,2

p = 5
q = 7
r = 10
s = 14

if a = 1
while p != q -> p+=1
if a = 2
while p!= q || p!= r -> p+=1

Also, this is a teach me to fish instead of giving me the fish type question.

Community
  • 1
  • 1
Lobstw
  • 489
  • 3
  • 18
  • you need a helper which returns boolean. – Jason Hu Mar 28 '16 at 00:04
  • Please don't double-post http://stackoverflow.com/questions/36025026/have-extra-while-loop-conditions-based-on-a-condition?lq=1 – MCMastery Mar 28 '16 at 00:12
  • 3
    @MCMastery That's python, this is java, and OP is explaining why that approach (supposedly) doesn't work in java – fps Mar 28 '16 at 00:16
  • 1
    Are the parameters to the conditions consistent? If so, what are they/is it? – Bohemian Mar 28 '16 at 00:25
  • @Bohemian do you mean condition_1, _2, etc? Condition 1 would be `true` if some random number n is in a slice of an array. Condition 2 would be `true` if the same random number n is in a different slice of the array. The different `while` loops are used because the different slice might not exist yet or be useful depending on value of `a` – Lobstw Mar 28 '16 at 00:52
  • @lob either answer my question (which you didn't), or edit your question to show actual code for you conditions. Depending on your conditions, there may be a neat way to do it. – Bohemian Mar 28 '16 at 01:38
  • @Bohemian Please clarify your original question – Lobstw Mar 28 '16 at 01:41
  • @lob the best and simplest thing is to post real Java code for the conditions. If they conditions can be made to have the same inputs, there is a straight forward way to do it elegantly – Bohemian Mar 28 '16 at 01:45
  • @lobstw still no Java code, so I can't say – Bohemian Mar 29 '16 at 21:20
  • 1
    @Lobstw The condition code fragments are different enough and require too many variables to elegantly be refactored as lamdbas. I was thinking of a Map of int to BiFunction then passing i and j to what was found using 0,1 or 2 as the key, but it would take a lot of work and be messier than what's been proposed (the switch or similar). – Bohemian Mar 30 '16 at 06:55

2 Answers2

1

Let's say you have a condition list defined.

boolean[] condition = new boolean[n];

a is the number of booleans you wish to check, so just create a new variable

boolean valid = false;


//check all conditions up to a
for (int i = 0; i < a; i++) {
    if (condition[i]) {
        valid = true;
        break;
    }
}

while (valid) {
    //perform action and then run the checking valid code again
}

In this case, you will be checking for a conditions in total, which is what you want.

0

remind you java switch supports fall through. you will need a helper function to do it, since switch-case in java is statement instead of expression:

private boolean helper(a) {
    boolean b = false;
    switch (a) {
        case 2: b = b || condition_3;
        case 1: b = b || condition_2;
        case 0: b = b || condition_1;
    }
    return b;
}

i can't really think of any case where you can't put a helper function. if that's true, i would like to learn about it. (in fact in python's case, the answer also suggests you to use helper(in lambda form))

Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • What if `a` is a much larger number like 500? – questioner and answerer Mar 28 '16 at 00:20
  • then you spend your time on writing 500 different conditions without any willingness to write a few more `case n`s? – Jason Hu Mar 28 '16 at 00:21
  • I meant in a case where you have a program to calculate the conditions in something like an array instead of manually typing them all out. – questioner and answerer Mar 28 '16 at 00:23
  • depending on how you think about it, `condition_x` here really means more than a variable. – Jason Hu Mar 28 '16 at 00:25
  • Thank you for your response, too. However, your code isn't compiling for me? It complains about the `||=` with "Expression expected" And because it's in a separate method, it doesn't know about some of the variables used in condition – Lobstw Mar 28 '16 at 01:08
  • @Lobstw screw me. java hasn't `||=`, please use `b = b || condition`. let me correct. also notice this one does short circuiting. it should work very nicely. – Jason Hu Mar 28 '16 at 01:09
  • Ok, none of my past two comments submitted. Sorry, but I was a bit unclear with what I wanted, I've edited the post with my desired outcome. I'd like it if you'd have a look. Thank you – Lobstw Mar 28 '16 at 01:25
  • @Lobstw if all those variables represent something, it's better to have them into an object, so my helper here actually applies very nicely. – Jason Hu Mar 28 '16 at 01:27
  • Ahh I see what you're saying. Working solution +1, I ended up passing all of my variables into your helper method but other than the solution works as is. THank you – Lobstw Mar 28 '16 at 01:45
  • Just realised how smart it was to flip the cases and use no break statement. Great answer. PS. The `b ||` part isn't necessary – Lobstw Mar 28 '16 at 15:34
  • @Lobstw this can only be done in languages those support fall through. – Jason Hu Mar 28 '16 at 15:51
  • slightly off topic, but is there a particular benefit to having switch and case fall through like that by default? I would have though the whole point of switch was to switch to one track and not go back for the others. – Lobstw Mar 28 '16 at 15:53
  • 1
    @Lobstw just one of the irreversible language design decisions. I think it's more about to be consistent with c, nothing else. C# has the same syntax but even force you to break or return. – Jason Hu Mar 28 '16 at 15:57
  • to be honest, one day SO will die because of these silent down voters. those are so bad. every time i encounter this i feel seriously pissed. – Jason Hu Mar 30 '16 at 00:22