tags:

views:

30

answers:

1

In Java, you sometimes do something like this:

for (int a = 1, b = 2; b < high;) {
    if (b % 2 == 0) {
        result += b;
    }

    int tmp = b;
    b       = a + b;
    a       = tmp;
}

Here, I used a for loop instead of a while loop to limit the scope of a and b.

But how can I achieve this in JavaFX? The for loop doesn't seem to offer this possibility. Do I have to use a while loop?

+1  A: 

You could use the Java trick of anonymous blocks:

var high = 10;

{
    var a = 0;
    for (b in [1..high-1]) {
      // this is fine
      println("{a}");
    }
}
// won't compile here
//println("{a}");

The are simmilar expressions in JavaFX but with those expressions you will get a double loop. According to this doc.

Karussell
Perfect, didn't think of using a block, thx.
Helper Method