Sometimes I need to write a loop in C/C++ where there is an initialization followed by a condition.
I like how this looks,
for (int i = 0, j = 0; i == 0 && j == 0;) {
// condition fails at some point
}
more than this.
int i = 0, j = 0;
while (i == 0 && j == 0) {
// condition fails at some point
}
Granted that this is personal code so noone can judge me. Will the compiler make it so that they are the same thing?
Use the for loop if you don't need the variables i and j after the loop.
Use the while loop if you need the variables i and j after the loop.
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