Back to C Programming
2026-07-135 min read

Loop directives

Learn Loop directives step by step with clear examples and exercises.

Title: Mastering C Loop Directives: A full guide for Practical Programming

Why This Matters

Loop directives, specifically break and continue, are essential tools in a C programmer's arsenal. They enable you to control the flow of your loops, making your code more efficient and adaptable. Understanding these directives will help you solve real-world programming problems, ace coding interviews, and even debug common issues that may arise during your coding journey.

Prerequisites

Before diving into loop directives, it is crucial to have a solid understanding of the following:

  1. Basic C syntax, including variables, data types, and operators
  2. Control structures like if, else, and conditional statements
  3. Understanding of loops, particularly for loops
  4. Familiarity with arrays and pointers (to better understand some examples)

Core Concept

While Loops

While loops are similar to for loops but have less functionality. They continue executing the while block as long as the condition in the while remains true. For example:

int n = 0;
while (n < 10) {
n++;
}

This code will execute exactly ten times, incrementing n each time.

Infinite Loops and Loop Directives

While loops can potentially run indefinitely if a condition is always true. To break out of such a loop, you can use the break directive:

int n = 0;
while (1) { // infinite loop
n++;
if (n == 10) {
break; // exit the loop when n reaches 10
}
}

The continue directive can be used to skip iterations in a loop. For example, to print only even numbers:

int n = 0;
while (n < 10) {
n++;
/* check that n is odd */
if (n % 2 == 1) { // n is odd, skip this iteration
continue;
}
/* we reach this code only if n is even */
printf("The number %d is even.\n", n);
}

Do-While Loops

A do-while loop is similar to a while loop, but the body of the loop is executed at least once before checking the condition. This can be useful when you know that the loop should run at least once:

int n = 0;
do {
printf("The number %d.\n", n);
n++;
} while (n < 10); // exit the loop when n reaches 10

Worked Example

Let's create an array of ten numbers and print those that are greater than 5 or less than 10.

#include <stdio.h>

int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int i = 0;
while (i < 10) { // loop through the array
if (array[i] > 10 || array[i] < 5) { // check the current number
printf("The number %d is neither greater than 10 nor less than 5.\n", array[i]);
}
i++; // advance to the next number in the array
}
return 0;
}

Extended Worked Example: Nested Loops and Array Manipulation

Let's create a program that finds the sum of all even numbers in an array. We will also sort the array before calculating the sum to ensure better efficiency.

#include <stdio.h>
#include <stdlib.h>

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void sortArray(int array[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (array[i] > array[j]) {
swap(&array[i], &array[j]);
}
}
}
}

int main() {
int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
int size = sizeof(array) / sizeof(array[0]);
sortArray(array, size);

int sum = 0;
for (int i = 0; i < size; i++) {
if (array[i] % 2 == 0) { // check if the number is even
sum += array[i]; // add it to the total sum
}
}
printf("The sum of all even numbers in the sorted array is: %d\n", sum);
return 0;
}

Common Mistakes

  1. Forgetting to increment the loop variable: This can cause an infinite loop if the condition never changes, or skipping numbers if the condition is checked too early.
  2. Misusing break and continue: These directives should be used judiciously and only when necessary to avoid creating complex and hard-to-debug code.
  3. Ignoring the order of operations: Remember that operators like == have higher precedence than others, so parentheses may sometimes be needed to correctly evaluate conditions.
  4. Not initializing loop variables: Always initialize your loop variables before using them in the loop condition. This can help avoid unexpected behavior and simplify debugging.
  5. Using break or continue within nested loops: Be careful when using these directives inside nested loops, as they may cause unintended consequences.

Practice Questions

  1. Write a program using a while loop and the continue directive to print all even numbers between 1 and 50.
  2. Modify the worked example to also exclude numbers that are multiples of 3.
  3. Create a do-while loop that asks the user for their name until they enter "John".
  4. Write a program using nested loops to find the sum of all even numbers in an array. The array should be sorted before calculating the sum.
  5. Write a program that uses both break and continue to implement a simple game where the user guesses a random number between 1 and 100. The program should provide hints based on whether the guessed number is too high or too low.

FAQ

What happens if I use break inside a for loop?

Breaking out of a for loop works similarly to breaking out of a while loop, causing the loop to terminate immediately.

Can I use continue with any type of loop in C?

Yes, both break and continue can be used with for loops, while loops, and do-while loops.

Is it possible to nest while loops in C?

Yes, you can nest while loops as long as the nested loop does not interfere with the execution of the outer loop.

How can I find the maximum number in an array using a while loop?

You can use a while loop and two variables (one for the current maximum and another for indexing) to iterate through the array and find the maximum number. Here's an example:

int max = array[0];
int i = 1;
while (i < size) {
if (array[i] > max) {
max = array[i];
}
i++;
}