M
M
Mikhri2021-01-24 06:56:32
Java
Mikhri, 2021-01-24 06:56:32

Why can't Java initialize variables of different types in a for clause?

?Why can't you create a structure like

for(int i = 1, double j = 1.0; i<=10, j<=9.99; i++, j++) {}

But you can
for(double i = 1.0, j = 2.8; i<=10.0 || j<=9.99; i++, j+=0.38) {}

Why do the variables initialized in the first block of conditions of the for loop have to be of the same type?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-01-24
@Mikhri

Probably because you can't initialize variables this way outside of the for loop.
You can't write:
int a, double d
But you can like this:
int a; double d;
And in the cycle, the semicolon will already be the division of the block.
If you really feel like iterating over two variables, you can get around this like this:

int i = 1;
double j = 1.0;
for(; i<=10, j<=9.99; i++, j++) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question