Back to C Programming
2026-07-116 min read

C break and continue

Learn C break and continue step by step with examples for Indian students.

Why This Matters

Understanding how break and continue work is crucial for controlling flow within loops (specifically while-loops) when writing C programs, especially during competitive exams like CS or campus placements where time management skills are tested.

In real-world applications such as embedded systems programming in Indian industries — think smart home devices controlled through a central hub using an RTOS (Real-Time Operating System), break and continue can help manage complex loops efficiently. These constructs allow programmers to exit the loop prematurely (break) or skip certain iterations without exiting completely, thus optimizing performance.

During exams like 's Computer Science courses where students are asked questions involving nested conditions within while-loops (e.g., checking for prime numbers), knowing how break and continue work can significantly improve problem-solving efficiency. In these scenarios, a student might need to exit the loop early upon finding that no further checks would yield results or skip unnecessary iterations.

Prerequisites

Before diving into this lesson on C's break and continue statements in while-loops, you should have:

  1. Basic knowledge of programming concepts like variables, data types (int, float), loops (for, while).
  2. Familiarity with conditional branching using if-else constructs.
  3. Understanding the concept of nested structures — blocks within other block(s) such as functions or control statements inside a loop.

Core Concept

In C language programming for competitive exams like , understanding how to use break and continue in while-loops is essential due to their unique behavior compared with break/continue constructs found elsewhere.

The Break Statement

The break statement immediately terminates the loop it resides within — be that a while-loop or any other type of looping construct like for, do-while loops.

When you encounter an infinite loop scenario (a common mistake in competitive exams), using break can save time and prevent endless execution. For instance:

int i = 0;
while(i < 10) {
if(i == 5)
break; // Exit the while-loop when 'i' equals to 5.
printf("%d ", i);
i++;
}

In this example, we print numbers from 0 up until it reaches a certain condition (if (i == 5)), after which control breaks out of loop. This is particularly useful in competitive exams where you might need the program to exit early for efficiency.

The Continue Statement

The continue statement skips all remaining code within its enclosing while-loop and moves directly back into this loop's start point — essentially skipping over any iterations that don't meet a specified condition but still continuing execution of subsequent statements.

For example, when checking if numbers in an array are even:

int arr[] = {2, 4, 6, 7, 8};
for(int i=0; i < sizeof(arr)/sizeof(arr[0]); ++i) {
if (arr[i] % 2 != 0)
continue;
printf("%d ", arr[i]);
}

In this example with an array of numbers [2,4,6,7,8], the loop continues until it finds a number that is not even. When continue executes upon encountering odd-numbered elements (like '7'), control skips to next iteration without executing remaining code in current one.

Worked Example

Let's consider this practical problem: You have an array of integers and you need to find all prime numbers within the range [1, 100]. A common mistake here is not checking for primes correctly or inefficiently looping through possible divisors.

#include <stdio.h>
int main() {
int arr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
int i = 0;

while(i < sizeof(arr)/sizeof(arr[0])) {
if (arr[i] % 2 == 1) { // Check for odd numbers only
printf("%d ", arr[i]);
}
++i;
}

return 0;
}

In this example, we have an array containing all prime numbers up to 97. We use a while-loop with continue statement inside it. The loop checks if the current number is odd (prime candidates only) and prints them out.

Common mistakes:

  1. Not correctly initializing variables.
  2. Forgetting incrementing index variable (i).
  3. Misusing break/continue statements within nested loops or functions without proper context awareness, leading to premature termination of execution flow which can cause bugs in competitive exams like 's CS courses where precision is key.

Common Mistakes

1. Improper Use of Break Statement

A common mistake when using break statement inside a while-loop during an exam scenario (like ) could be prematurely exiting the loop without fulfilling all conditions or missing out on necessary iterations for correct results, especially if not used properly in nested loops.

Example:

int i = 0;
while(i < 10) {
printf("%d ", i);
break; // Mistakenly exits after first iteration.
}

2. Misuse of Continue Statement with Nested Loops or Functions

Another common mistake is using continue statement outside the right context, like within nested loops without proper conditions leading to skipping necessary iterations.

Example:

int arr[] = {1, 3, 5};
for(int i=0; i < sizeof(arr)/sizeof(arr[0]); ++i) {
if (arr[i] % 2 == 0)
continue;
for(int j=i+1; j < sizeof(arr)/sizeof(arr[0]); ++j){
printf("%d ", arr[j]);
}
}

In this example, the inner loop is skipped entirely due to misplaced continue statement which leads incorrect output.

Practice Questions

Question 1

Write a C program using while-loop with break and continue statements that prints all even numbers from an array of integers up to size specified by user input.

Example:

#include <stdio.h>
int main() {
int arr[] = {2, 4, 6, 7, 8};
int n;

printf("Enter the number of elements: ");
scanf("%d", &n);

for(int i=0; i<n && i<sizeof(arr)/sizeof(arr[0]); ++i) {
if (arr[i] % 2 != 0)
continue;

printf("%d ", arr[i]);

// Break when encountering number greater than user specified limit
int max_limit;
scanf("%d", &max_limit);
if(max_limit < arr[i])
break;
}

return 0;
}

Question 2

Write a C program using while-loop with continue statement that prints all odd numbers from an array of integers up to size specified by user input.

Example:

#include <stdio.h>
int main() {
int arr[] = {1, 3, 5, 7};
int n;

printf("Enter the number of elements: ");
scanf("%d", &n);

for(int i=0; i<n && i<sizeof(arr)/sizeof(arr[0]); ++i) {
if (arr[i] % 2 == 0)
continue;

printf("%d ", arr[i]);

// Break when encountering number greater than user specified limit
int max_limit;
scanf("%d", &max_limit);
if(max_limit < arr[i])
break;
}

return 0;
}

FAQ

Q1: What is the difference between using break and continue statements within a while-loop?

A1: The primary distinction lies in their control flow. A continue statement skips to next iteration of loop without executing remaining code for current one, whereas break terminates entire enclosing block (loop or function) prematurely.

Q2: Can you use both 'break' & 'continue' inside the same nested loops?

A2: Yes but with caution as it can lead complex flow control issues. It's recommended to avoid such scenarios unless absolutely necessary for performance optimization in competitive exams like CS where time efficiency is crucial.

Q3: Why are break and continue statements essential when dealing with nested loops?

A3: They allow you fine-grained control over flow of execution, enabling efficient termination or skipping iterations based on specific conditions. This becomes particularly useful in competitive exams like 's CS courses where students must optimize their code for performance while ensuring correctness.

Q4: What are some common mistakes to avoid when using break and continue statements?

A4: Avoiding premature loop exit, misusing continue statement within nested loops or functions without proper conditions can lead to incorrect results. Also refrain from overuse of these constructs as it may result in unnecessarily complex code which is difficult for examiners like CS graders.

Q5: How do you ensure that your use of break and continue statements doesn't affect loop termination or skipping iterations unexpectedly?

A5: Always check the conditions before using break/ continue. Keep track of nested loops, avoid unnecessary complexity in code logic for easier readability & debugging during competitive exams like CS where examiners look at both correctness as well as coding style.

To summarize understanding and correctly utilizing break and continue statements within while-loops is essential to control flow efficiently. Avoid common mistakes such as misusing these constructs or overcomplicating code logic which can lead to incorrect results during competitive exams like CS , 's Computer Science courses etc.

Remember always keep your C programming skills sharp by practicing with real-world examples and exam-style questions!