Introduction
Loop control helps manage how many times a loop runs. It stops loops from running forever and lets you skip or end loops early.
Loop control helps manage how many times a loop runs. It stops loops from running forever and lets you skip or end loops early.
for (initialization; condition; update) { // code to repeat } while (condition) { // code to repeat } // Loop control statements: break; // stops the loop continue; // skips to next loop cycle
break stops the loop immediately.
continue skips the rest of the current loop and moves to the next cycle.
for (int i = 0; i < 5; i++) { if (i == 3) { break; } printf("%d ", i); }
for (int i = 0; i < 5; i++) { if (i == 3) { continue; } printf("%d ", i); }
This program prints odd numbers less than 5. It skips even numbers and stops when i reaches 5.
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i == 5) { break; // stop loop when i is 5 } if (i % 2 == 0) { continue; // skip even numbers } printf("%d ", i); } return 0; }
Without loop control, loops might run forever and crash your program.
Use break to exit loops early when you find what you need.
Use continue to skip steps you don't want to run inside the loop.
Loop control helps manage how loops run and stop.
break stops the loop immediately.
continue skips to the next loop cycle.
break and continue help manage loop execution by stopping or skipping iterations.break; is used to exit a loop immediately.continue; skips to next iteration, exit; is not a valid statement, and stop; does not exist.for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
printf("%d ", i);
}i == 3, continue; skips printing 3.int i = 0;
while(i < 5) {
if(i == 3)
break
printf("%d ", i);
i++;
}break statement must end with a semicolon (;), but it is missing here.i is initialized, break is valid inside while, and printf syntax is correct.continue; skips the current iteration, so placing it when number is multiple of 3 skips printing those numbers.Pass the test and this pattern moves unknown