Introduction
The continue statement is a control flow statement in C that skips the remaining statements in the current iteration of a loop and jumps to the next iteration. Unlike break which exits the loop entirely, continue only skips the current iteration and allows the loop to continue with the next cycle.
Key Concepts
Skip Current Iteration: Bypasses remaining statements in current loop cycle Next Iteration: Jumps directly to loop condition check Loop Continuation: Loop continues running, unlike break Inner Loop Only: Affects only the innermost loop in nested structures
Basic Syntax
continue;
Continue in Different Loops
Continue in For Loop
#include <stdio.h>
int main() {
printf("Numbers 1 to 10, skipping even numbers:\n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Continue in While Loop
#include <stdio.h>
int main() {
int i = 0;
printf("Printing odd numbers from 1 to 10:\n");
while (i < 10) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Continue in Do-While Loop
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Enter numbers (0 to stop). Negative numbers will be ignored:\n");
do {
scanf("%d", &number);
if (number < 0) {
printf("Negative number ignored!\n");
continue; // Skip negative numbers
}
if (number == 0) {
break; // Exit loop
}
sum += number;
printf("Current sum: %d\n", sum);
} while (1);
printf("Final sum: %d\n", sum);
return 0;
}
Practical Examples
Processing Valid Inputs Only
#include <stdio.h>
int main() {
int grades[10];
int count = 0;
int total = 0;
printf("Enter 10 grades (0-100). Invalid grades will be skipped:\n");
for (int i = 0; i < 10; i++) {
int grade;
printf("Grade %d: ", i + 1);
scanf("%d", &grade);
// Skip invalid grades
if (grade < 0 || grade > 100) {
printf("Invalid grade! Skipping...\n");
continue;
}
grades[count] = grade;
total += grade;
count++;
}
if (count > 0) {
printf("Average of %d valid grades: %.2f\n", count, (float)total / count);
} else {
printf("No valid grades entered!\n");
}
return 0;
}
Filtering Array Elements
#include <stdio.h>
int main() {
int numbers[] = {1, -3, 5, -7, 9, 0, 4, -2, 8, 6};
int size = 10;
int positive_sum = 0;
int positive_count = 0;
printf("Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
printf("Processing positive numbers only:\n");
for (int i = 0; i < size; i++) {
if (numbers[i] <= 0) {
continue; // Skip non-positive numbers
}
printf("Adding %d to sum\n", numbers[i]);
positive_sum += numbers[i];
positive_count++;
}
printf("Sum of positive numbers: %d\n", positive_sum);
printf("Count of positive numbers: %d\n", positive_count);
return 0;
}
Prime Number Checking
#include <stdio.h>
int main() {
printf("Prime numbers between 2 and 30:\n");
for (int num = 2; num <= 30; num++) {
int is_prime = 1; // Assume prime
// Check if number has divisors other than 1 and itself
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
is_prime = 0; // Not prime
break;
}
}
if (!is_prime) {
continue; // Skip non-prime numbers
}
printf("%d ", num);
}
printf("\n");
return 0;
}
Menu System with Input Validation
#include <stdio.h>
int main() {
int choice;
int running = 1;
while (running) {
printf("\n=== MENU ===\n");
printf("1. Option A\n");
printf("2. Option B\n");
printf("3. Option C\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice < 1 || choice > 4) {
printf("Invalid choice! Please try again.\n");
continue; // Skip to next iteration for new input
}
switch (choice) {
case 1:
printf("You selected Option A\n");
break;
case 2:
printf("You selected Option B\n");
break;
case 3:
printf("You selected Option C\n");
break;
case 4:
printf("Goodbye!\n");
running = 0;
break;
}
}
return 0;
}
Continue in Nested Loops
Continue in Inner Loop
#include <stdio.h>
int main() {
printf("Multiplication table (skipping multiples of 3):\n");
for (int i = 1; i <= 5; i++) {
printf("Table of %d: ", i);
for (int j = 1; j <= 10; j++) {
int product = i * j;
if (product % 3 == 0) {
continue; // Skip multiples of 3
}
printf("%d ", product);
}
printf("\n");
}
return 0;
}
Processing 2D Array
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, -2, 3},
{-4, 5, -6},
{7, 8, 9}
};
int positive_sum = 0;
printf("Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
printf("\nProcessing positive elements only:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] <= 0) {
continue; // Skip non-positive elements
}
printf("Adding element [%d][%d] = %d\n", i, j, matrix[i][j]);
positive_sum += matrix[i][j];
}
}
printf("Sum of positive elements: %d\n", positive_sum);
return 0;
}
Difference Between Break and Continue
Comparison Example
#include <stdio.h>
int main() {
printf("Using BREAK (exits loop):\n");
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exits loop completely
}
printf("%d ", i);
}
printf("Loop ended\n\n");
printf("Using CONTINUE (skips iteration):\n");
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skips only this iteration
}
printf("%d ", i);
}
printf("Loop completed\n");
return 0;
}
Common Use Cases
Data Validation
#include <stdio.h>
int main() {
int ages[5];
int valid_count = 0;
printf("Enter 5 ages (must be between 1-120):\n");
for (int i = 0; i < 5; i++) {
int age;
printf("Age %d: ", i + 1);
scanf("%d", &age);
if (age < 1 || age > 120) {
printf("Invalid age! Skipping...\n");
i--; // Retry this input
continue;
}
ages[valid_count] = age;
valid_count++;
}
printf("Valid ages entered: ");
for (int i = 0; i < valid_count; i++) {
printf("%d ", ages[i]);
}
printf("\n");
return 0;
}
Common Mistakes
Continue Outside Loop
// Wrong - continue outside any loop
if (condition) {
continue; // Compilation error!
}
// Correct - continue inside loop
for (int i = 0; i < 10; i++) {
if (condition) {
continue; // This is valid
}
}
Infinite Loop with Continue
// Dangerous - can cause infinite loop
int i = 0;
while (i < 10) {
if (some_condition) {
continue; // i is never incremented!
}
i++;
}
// Correct - ensure loop variable is updated
int i = 0;
while (i < 10) {
i++; // Update before continue
if (some_condition) {
continue;
}
// other processing
}
Best Practices
- Use for Filtering: Continue is ideal for skipping unwanted elements
- Avoid Complex Logic: Keep continue conditions simple and clear
- Update Loop Variables: Ensure loop counters are updated before continue
- Comment Intent: Explain why certain iterations are skipped
- Consider Alternatives: Sometimes restructuring conditions is clearer
Summary
The continue statement provides a way to skip the current iteration of a loop and proceed to the next one. It’s particularly useful for filtering data, handling invalid inputs, and processing only specific elements in collections. Unlike break which terminates the entire loop, continue allows selective processing while maintaining loop execution. Proper use of continue can make code cleaner and more efficient by avoiding unnecessary processing of unwanted data elements.
Part of BCA Programming with C Course (UGCOA22J201)