Ch 7 · Loops & Iterations · C
Topic 7 of 13 in C — Complete Tutorial (Beginner) — 3 lessons.
The for Loop
A loop runs a block of code many times automatically. The for loop is ideal when you know how many repetitions you need. It has three parts on one line: where to start, the condition to keep going, and how to change the counter each time. C checks the condition before each repetition; when it becomes false (zero), the loop ends.
while and do-while
The while loop repeats as long as a condition stays true, and you change the condition yourself inside the loop. It is best when you do not know in advance how many repetitions you need. The do-while loop checks its condition *after* running the body, so the body always runs at least once. The golden rule: make sure the condition will eventually become false, or you create an infinite loop.
break and continue
Inside any loop, break exits immediately — handy once you have found what you were searching for. continue skips the rest of the current repetition and jumps to the next one — useful for ignoring certain values while processing the rest. Both give you fine control over how a loop flows.