Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: May 19, 2024
Difficulty: Medium

Continue Statement in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

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;
}

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.

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.

C break and continue Statement Differences

While both 'break' and 'continue' statements are used to alter the flow of loops in C, they have distinct differences in their functionality:

Loop Termination

  • The 'break' statement terminates the entire loop prematurely and transfers the control to the next statement following the loop.
     
  • The 'continue' statement only skips the current iteration of the loop and moves to the next iteration, without terminating the loop entirely.

Scope of Execution

  • When a 'break' statement is encountered, it immediately exits the loop, and the program continues with the next statement after the loop.
     
  • When a 'continue' statement is encountered, it skips the remaining code in the current iteration and moves to the next iteration of the loop.

Usage

  • The 'break' statement is commonly used when you want to exit a loop based on a certain condition, such as when a specific value is found or a condition is met.
     
  • The 'continue' statement is used when you want to skip certain iterations of a loop based on a condition, without terminating the entire loop.

Applicability

  • The 'break' statement can be used with 'for', 'while', 'do-while' loops, and 'switch' statements.
     
  • The 'continue' statement can only be used within loops ('for', 'while', 'do-while').

Control Flow

  • When a 'break' statement is executed, the control is transferred to the next statement following the loop.
     
  • When a 'continue' statement is executed, the control is transferred to the next iteration of the loop, skipping the remaining code in the current iteration.

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;
}

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.

Topics covered
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.
How the continue Statement Works
7.
Flowchart of continue in C
7.1.
In this flowchart:
8.
C break and continue Statement Differences
8.1.
Loop Termination
8.2.
Scope of Execution
8.3.
Usage
8.4.
Applicability
8.5.
Control Flow
9.
Example to Demonstrate the Difference Between break and continue Statement : 
9.1.
Problem Statement
9.2.
C
9.3.
Explanation
10.
Frequently Asked Questions
10.1.
Can continue be used outside a loop?
10.2.
Is it possible to use continue in an infinite loop?
10.3.
How does continue differ from goto in controlling flow?
11.
Conclusion