helpermethod
helpermethod

Reputation: 62185

Declaring variables for limited scope at the head of the for loop

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?

Upvotes: 0

Views: 84

Answers (1)

Karussell
Karussell

Reputation: 17375

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.

Upvotes: 1

Related Questions