mercredi 19 août 2020

Why we can have a semicolon in if but not in while loop

Why can I do this:

if (int result=getValue(); result > 100) {
}

but cannot do this:

while (int result=getValue(); result > 100) {
}

Why discriminate against while? A condition is a condition. Why while cannot evaluate it like if can?

In order to achieve the desired behavior with while, I'd have to implement it this this way:

int result = getValue();
while (result > 100) {
    //do something
    result = getValue();
}

Aucun commentaire:

Enregistrer un commentaire