← Back to C Course | Chapter 4: Control Flow | Lesson 11 of 11

C break & continue

break leaves a loop right away, like walking out of a game once you win, and continue skips only the current round. They give you control inside repeating steps.
Syntax
c
break;     // leave the loop immediately
continue;  // skip to the next iteration

The break Statement in Loops

break immediately exits the loop it's inside, skipping any remaining iterations entirely -- execution jumps straight to the first statement after the loop, regardless of what the loop's original condition would have done.

Example: The break Statement in Loops

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 10; i++) {
		// Check whether i == 5
		if (i == 5) {
			// Leave the switch/loop
			break;
		}
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

The continue Statement in Loops

continue skips only the rest of the current iteration's body, then jumps straight to the loop's update step and condition check to begin the next iteration -- unlike break, the loop itself keeps running.

Example: The continue Statement in Loops

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 5; i++) {
		// Check whether i == 3
		if (i == 3) {
			// Skip the rest of this iteration
			continue;
		}
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using break with while Loops

Inside a while loop, break is commonly used to exit as soon as a specific condition is detected mid-loop, giving you a clean way to stop early instead of needing to redesign the loop's main condition around that case.

Example: Using break with while Loops

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare i and set it to 1
	int i = 1;
	// Repeat while i <= 10 is true
	while (i <= 10) {
		// Check whether i == 4
		if (i == 4) {
			// Leave the switch/loop
			break;
		}
		// Print formatted text to the screen
		printf("%d ", i);
		i++;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using continue with while Loops

When using continue inside a while loop, you must make sure the loop's counter or condition variable is updated before the continue statement runs, or you risk creating an infinite loop that keeps skipping the update step.

Example: Using continue with while Loops

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare i and set it to 0
	int i = 0;
	// Repeat while i < 5 is true
	while (i < 5) {
		i++;
		// Check whether i == 3
		if (i == 3) {
			// Skip the rest of this iteration
			continue;
		}
		// Print formatted text to the screen
		printf("%d ", i);
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Loop Breaks

break only exits the single loop it's physically written inside -- if that loop is nested inside another one, the outer loop is completely unaffected and keeps running its own iterations normally.

Example: Nested Loop Breaks

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 3; j++) {
			// Check whether j == 1
			if (j == 1) {
				// Leave the switch/loop
				break;
			}
			// Print formatted text to the screen
			printf("(%d,%d) ", i, j);
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using break and expecting it to exit both loops in a nested loop, when it only exits the innermost.
  2. Putting continue before the increment in a while loop, causing an infinite loop.
  3. Using break outside a loop or switch, which is a compile error.
Chapter Summary
  • if, if-else, else-if ladders, nested conditions, and switch let a program choose between different paths.
  • while, do-while, and for loops, including nested loops, repeat code while a condition holds.
  • break and continue give finer control by leaving a loop or skipping to its next iteration.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.