Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Declaring a variable in a loop or if statement is supported since C99: https://en.wikipedia.org/wiki/C99

Also in Java: https://www.geeksforgeeks.org/for-loop-java-important-points...



Regarding Java:

It is possible that you confuse while- and for statements?


No, declaring a variable in a `for` loop is supported in C99 but you can't do if-init.


You do :)

My post was about while and if and you repeatingly bring up for. The for statement is another statement.


Ah, you're right (finally tried it out). But it 100% works in a loop (usually for loop)


It’s one of those things that I never knew I wanted until I started using it, and now i miss it when it’s not available.The reason you want it is the same reason you want to declare a variable in the statement of a for loop rather than pre declaring and using a while loop


I've used it in go a lot. If you want to kludge it, you can still put a block around the if. But it's not nearly as nice




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: