In PHP, Java, C++ (and many other languages), for loops are used like this:
for(int i=0;i<10;i++)
If I already initialized i
, how can I omit the initialization statement?
In Java, C++ and PHP it is completely valid to omit the initialization portion of the for
loop
int i = 0;
...
for(; i < 10; i++);
This is true of most languages which have a for
loop structure
for(; i < 10; i++) {
...
}
You can leave out any of the items in the for loop if they are not needed. You could also put in multiple things to do, or multiple conditions to check such as:
int j = 40;
for(int i = 0; i < 10 || j > 30; i++, j--) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With