2

I know it is easily achievable through an if/else-statement with two different for-loops, but what I'm asking about is if it's possible to do something like:

for(int a = 0; a < value ; boolean ? a++ : a--){
}

But that only leaves the error "not a statement" in my compiler.

EDIT: The a<value isn't a big issue. I'm okay with this being an infinite loop in both directions with a break-condition inside the for-loop.

Andreas Evjenth
  • 500
  • 3
  • 8
  • did you try it?? let us know.. – ΦXocę 웃 Пepeúpa ツ Jun 28 '16 at 17:51
  • 2
    `boolean` is a type. Not a variable. – Logan Jun 28 '16 at 17:52
  • @LoganKulinski I know. But I want the int a to increase or decrease based on the boolean. – Andreas Evjenth Jun 28 '16 at 17:53
  • @ΦXocę웃Пepeúpa Yes. I tried it. The compile gave me an error. – Andreas Evjenth Jun 28 '16 at 17:53
  • @AndreasEvjenth Why not try it with a variable? – Logan Jun 28 '16 at 17:53
  • It sounds like you don't want a for loop at all. What exactly are you trying to do? (See also http://stackoverflow.com/a/26158610/1743880) – Tunaki Jun 28 '16 at 17:53
  • @Tunaki in short, I'm copying an sub-array from an array of about 400 elements. It starts at a specific point and copies 4 elements, but if it starts at 398 it will copy number 398, 399, 400 and then throw an outOfBoundException, so instead I want to copy 398, 397, 396 and 395. I know this is easy with an if/else statement with two different for-loops, but I was wondering if it was possible with 1 for-loop with an boolean condition. – Andreas Evjenth Jun 28 '16 at 17:59
  • Even if it would work, if bool == false, depending on value your loop would either be run forever (if value if greater than 0) or not at all (if value is <= 0). – Ridcully Jun 28 '16 at 18:03
  • @Ridcully that is correct, yes. But I'm not really interested in that. In fact, I'm okay with it being an infinite loop in both directions. It's just that the for-loop had some much inside it, that the whole method would be far more readable if it was just one of them. But most of all, I was just curious if it was possible at all, but struggled to find any good answers (perhaps I searched with the wrong keywords). – Andreas Evjenth Jun 28 '16 at 18:27

2 Answers2

10

Yes, you can technically use a ternary operator in the [ForUpdate] part of a for loop. The syntax for it would be:

for (int a = 0; a < value; a += bool ? 1 : -1){
    // ...
}

where bool is of type boolean. It will either increment or decrement a depending on whether bool is true or not.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

Yes, you can do it.

The third statement in a for-loop it's just an expression that evaluates once per iteration. You're getting a compilation error because the ternary operator needs an assignment in order to be valid.

boolean ? a++ : a--

Instead here's another way of doing the same

boolean b = true;
int a = 0;
for (; a < value; a = b ? a + 1 : a - 1) {
    //Your code
}

Hope this helps!

Pato94
  • 784
  • 5
  • 6