Table of contents
1.
Introduction
2.
What is continue in C?
3.
Syntax of continue in C
4.
Use of continue in C
4.1.
C
5.
Example of continue in C
5.1.
C
6.
C continue Statement with Inner Loop
7.
How the continue Statement Works
8.
Flowchart of continue in C
8.1.
In this flowchart:
9.
Difference between C break and continue Statement 
10.
Example to Demonstrate the Difference Between break and continue Statement : 
10.1.
Problem Statement
10.2.
C
10.3.
Explanation
11.
Frequently Asked Questions
11.1.
Can continue be used outside a loop?
11.2.
Is it possible to use continue in an infinite loop?
11.3.
How does continue differ from goto in controlling flow?
12.
Conclusion
Last Updated: Aug 26, 2024
Medium

Continue Statement in C

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C programming, the 'continue' statement is used to skip the current iteration of a loop & move to the next one. It allows you to control the flow of your program by bypassing certain parts of the code based on specific conditions. By using 'continue', you can optimize your loops & make your code more efficient. 

Continue Statement in C

In this article, we'll learn about the syntax, usage, & examples of the 'continue' statement in C. We'll also compare it with the 'break' statement to understand their differences.

What is continue in C?

In C programming, the continue statement is a control structure used to interrupt the current iteration of a loop without stopping the loop entirely. When continue is encountered within a loop, the remaining code in the loop for that iteration is skipped, & the loop proceeds to the next iteration. This statement is particularly useful when you want to skip specific parts of a loop under certain conditions, without breaking out of the loop.

For example, if you are processing a list of numbers & want to skip any negative numbers, you can continue to bypass the processing code for any negative value, while continuing with the other numbers in the list.

Syntax of continue in C

The syntax for the continue statement in C is straightforward. It consists solely of the keyword continue; placed within the body of a loop. This loop can be any of the typical loop constructs in C, such as for, while, or do-while.

Here’s how you typically use it in a loop:

for(int i = 0; i < 10; i++) {
    if (i % 2 == 0) {  // Condition to check for even numbers
        continue;       // Skip the rest of the loop for even numbers
    }
    printf("%d ", i);   // This statement will only execute for odd numbers
}


In this example, the continue statement is used within a for loop. The condition inside the if statement checks if the number (i) is even. If it is, the continue statement executes, which causes the loop to immediately start the next iteration, skipping the printf function for even numbers. Thus, only odd numbers are printed.

Use of continue in C

The continue statement is used in C to enhance the control of loop execution. It enables the program to skip certain parts of a loop when specific conditions are met, without exiting the loop completely. This is especially helpful in situations where certain data needs to be ignored or skipped over efficiently. Below are some common scenarios where continue can be effectively used:

  • Skipping Specific Elements:When iterating through an array or list, you might want to skip over certain elements based on a condition. For instance, in a list of student grades, you might want to perform operations only on grades that are above a certain threshold and skip all others.
     
  • Error Checking in Loops: Often, loops process data that might have occasional errors or unusual values. By using continue, you can skip processing for these erroneous data points without stopping the entire loop process.
     
  • Conditional Processing: In cases where only specific conditions warrant processing, continue helps by skipping the processing for non-qualifying conditions. This makes the loop more efficient by not wasting cycles on unnecessary processing.


Here’s a practical example to illustrate the use of continue:

  • C

C

#include <stdio.h>

int main() {
int numbers[] = {5, 2, 15, 20, -3, 18};
int size = sizeof(numbers) / sizeof(numbers[0]);

for(int i = 0; i < size; i++) {
if (numbers[i] < 10) {
continue; // Skip the remaining part of the loop if number is less than 10
}
printf("Number: %d\n", numbers[i]); // This line only executes for numbers 10 or greater
}
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Number: 15
Number: 20
Number: 18


In this code, the continue statement is used to skip printing numbers less than 10. Only the numbers that are 10 or greater are printed to the output. This is a simple but effective use of continue to control the flow of a loop based on specific conditions.

Example of continue in C

Consider a program that processes an array of integers and prints only those integers that are even. If the integer is odd, the loop will skip the printing step and continue with the next iteration.

  • C

C

#include <stdio.h>

int main() {
int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(numbers) / sizeof(numbers[0]);

for (int i = 0; i < size; i++) {
if (numbers[i] % 2 != 0) { // Check if the number is odd
continue; // Skip the rest of the loop body for odd numbers
}
printf("%d is even.\n", numbers[i]); // This line only executes for even numbers
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

2 is even.
4 is even.
6 is even.
8 is even.
10 is even.


In this code:

  • The loop iterates over each element of the array numbers.
     
  • The if statement checks whether the current number is odd using the modulus operator (%).
     
  • If the condition is true (the number is odd), the continue statement executes, causing the loop to immediately proceed to the next iteration, skipping the printf function call.
     
  • Only even numbers are printed because the printf function call is skipped whenever an odd number is encountered.

C continue Statement with Inner Loop

The continue statement in C is used to skip the remaining code inside a loop for the current iteration and proceed with the next iteration. When used within an inner loop, it affects only that inner loop and not the outer loop.

Explanation:

  • Purpose: The continue statement is often used to skip certain iterations based on specific conditions, allowing more control over the loop’s execution.
  • Inner Loop Behavior: When a continue statement is encountered in an inner loop, it skips the rest of the code inside the inner loop for the current iteration and proceeds with the next iteration of the inner loop.

How the continue Statement Works

The continue statement interacts with loops by altering the normal sequence of operations. Here's what happens when continue is used:

  1. Loop Initiation: First, the loop starts normally. This could be the initialization of a for loop or the condition check at the beginning of a while or do-while loop.
     
  2. Condition Check: As the loop executes, it reaches the condition that possibly includes the continue statement. This condition is checked during each iteration of the loop.
     
  3. Execution of continue: If the condition for continue is met, the loop skips all subsequent statements after continue and proceeds directly to the next iteration. This might involve incrementing the loop counter or re-evaluating the loop's condition.
     
  4. Skipping Statements: Any statements after the continue in the loop body are not executed for the current iteration. The loop does not terminate but instead immediately continues with the next iteration.
     
  5. Continuation of Loop: After skipping the statements, the loop increments (if it's a for loop) or re-checks the condition (if it's a while or do-while loop) and proceeds as normal.
     

The practical effect of continue is that it allows for selective processing within the loop, focusing on elements that meet specific criteria and skipping over those that do not, without breaking out of the loop structure. This can be particularly useful in loops where only certain items need detailed processing, allowing for efficient management of larger datasets or complex conditions.

Here's a simple conceptual representation:

  • Loop starts (initiate variables, if any)
     
  • Enter loop condition
     
  • If continue condition is met:
     
  • Skip remaining code in this loop iteration
     
  • Else:
     
  • Execute the rest of the code in the loop
     
  • Increment/Reevaluate condition
     
  • Repeat or end loop

Flowchart of continue in C

Here's a flowchart that illustrates the flow of control when using the 'continue' statement in a loop:

Flowchart of continue in C

In this flowchart:

  1. The loop begins, & the loop condition is checked.
     
  2. If the loop condition is true, the program enters the loop body.
     
  3. The loop body is executed until the 'continue' statement is encountered.
     
  4. When the 'continue' statement is reached, the program immediately jumps to the step where the loop counter or condition is updated.
     
  5. The loop counter or condition is updated, & the program moves back to the beginning of the loop.
     
  6. Steps 2-5 are repeated until the loop condition becomes false, & the loop terminates.
     

The 'continue' statement essentially skips the remaining code in the loop body & moves directly to the next iteration of the loop.

Difference between C break and continue Statement 

Parameters break Statement continue Statement
Purpose Exits the nearest enclosing loop or switch Skips the remaining code in the current iteration of the loop
Control Flow Transfers control to the statement following the loop or switch Transfers control to the next iteration of the loop
Usage in Loops Ends the loop entirely Proceeds to the next iteration of the loop without executing the remaining code in the loop body
Usage in Switch Case Exits the switch statement, optionally with a value Not used in switch statements
Impact on Loop Completely terminates the loop Skips the rest of the current loop iteration and continues with the next iteration
Example c\nfor (int i = 0; i < 10; i++) {\n if (i == 5) break;\n printf("%d ", i);\n}\n// Output: 0 1 2 3 4\n c\nfor (int i = 0; i < 10; i++) {\n if (i == 5) continue;\n printf("%d ", i);\n}\n// Output: 0 1 2 3 4 6 7 8 9\n

Example to Demonstrate the Difference Between break and continue Statement : 

To understand the operational differences between the break and continue statements in C programming, consider an example that processes an array of integers. The goal is to output each number unless it meets certain conditions, where either a break or a continue will be used to alter the loop's execution.

Problem Statement

  • Print all integers in an array sequentially.
     
  • Stop printing when an integer is equal to -1 (using break).
     
  • Skip printing an integer if it is zero (using continue).
  • C

C

#include <stdio.h>
int main() {
int numbers[] = {10, 0, 20, -1, 30, 0, 40};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Using break and continue:\n");
for (int i = 0; i < size; i++) {
if (numbers[i] == -1) {
printf("Encountered -1, stopping loop...\n");
break; // Stops the entire loop if the number is -1
}
if (numbers[i] == 0) {
printf("Skipping 0...\n");
continue; // Skips printing this iteration if the number is 0
}
printf("%d\n", numbers[i]); // Prints the number if it's neither -1 nor 0
}
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Using break and continue:
10
Skipping 0...
20
Encountered -1, stopping loop...

Explanation

  • The loop goes through each element of the numbers array.
     
  • When it encounters -1, the break statement is executed, and the loop stops immediately, as shown by the output message indicating the loop's end.
     
  • When it encounters a zero, the continue statement is executed, skipping the printing of the zero and continuing with the next loop iteration.

This code demonstrates two behaviors:

  • Stopping the loop: The break statement is used when a terminating condition is met (-1 in this case), and we need to stop all further processing.
     
  • Skipping specific iterations: The continue statement is used to avoid executing part of the loop for specific conditions (like skipping zeros) without exiting the loop.

Frequently Asked Questions

Can continue be used outside a loop?

No, the continue statement cannot be used outside a loop. If used outside, it will result in a compilation error, as continue is specifically designed to affect loop iterations.

Is it possible to use continue in an infinite loop?

Yes, continue can be used in infinite loops, such as while(true) or for(;;). It helps in skipping certain iterations based on specific conditions even when the loop is intended to run indefinitely.

How does continue differ from goto in controlling flow?

While both continue and goto can alter the flow of execution in a program, continue is specifically for skipping the remainder of a loop iteration and immediately continuing with the next iteration. In contrast, goto can jump to any part of the program specified by a label, making it more flexible but potentially harder to manage and understand in complex codes.

Conclusion

In this article, we have learned the fundamentals of the continue statement in C programming. We've covered its definition, syntax, practical uses, and provided examples that illustrate how to implement continue in different looping scenarios. Understanding the continue statement, along with its counterpart break, enables programmers to write more efficient and controlled loop operations, enhancing the overall logic and functionality of the program. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass