Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to have a for loop without an increment or decrement?

Tags:

c++

c

for-loop

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?

like image 367
YeOldeBitwise Avatar asked Jan 30 '26 02:01

YeOldeBitwise


1 Answers

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.

like image 138
R Sahu Avatar answered Jan 31 '26 15:01

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!