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.