Skip to main content
Advertisement

C: Loops

Used when you need to perform the same task multiple times.

1. for Loop

The most commonly used loop structure, ideal when the number of iterations is predefined.

// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
printf("Current value: %d\n", i);
}

2. while Loop

Continuously repeats as long as the specified condition remains true.

int count = 0;
while (count < 3) {
printf("Looping...\n");
count++;
}

3. break & continue

  • break: Immediately exits the loop.
  • continue: Skips the remainder of the current iteration and jumps to the next one.
Advertisement