Reach for for when the loop has a clear counter, a variable you initialize once, test each pass, and update each pass. for gathers all three in one header, so a reader takes in the entire loop-control machinery at a glance instead of hunting for the initializer above the loop and the increment somewhere inside the body. That co-location is also what prevents the classic bug of a continue skipping the update and hanging the loop. Prefer while when there's no meaningful count: consuming input until EOF, retrying until success, polling until a flag changes, walking a linked list until NULL. Two supporting points. Declaring the counter in the header (for (int i = 0; ...)) scopes it to the loop, so it can't leak into surrounding code or collide with a later i, which is a real advantage over a while with an externally declared counter. And do/while is the third option, worth naming because it's the one that guarantees the body executes at least once before testing, which is what you want for "send, then check whether it worked". The underlying rule is that the two are formally interchangeable, any for can be written as a while, so the choice is entirely about which one makes the loop's contract obvious to the next reader.
Programming Fundamentals · Interview question
When should you reach for for instead of while?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
for Loops, break & continue
The counted loop you'll write most often, plus the two escape hatches: break to leave early, continue to skip an iteration.