example of continue statement in c

Continue Examples in C: Understanding When to Use This Loop Statement

By
AD

The continue statement in C is a powerful tool that allows a loop to pick up where it left off in the next iteration, after skipping the rest of its current iterations. It's particularly useful when dealing with situations where an iteration needs to be exited early, but other remaining iterations should continue to run.

To illustrate this, let's look at a simple example. Consider a scenario where we're iterating through an array of integers and we want to stop processing the array once we find a certain value. Instead of using a *break* statement, which would exit the loop entirely, we can use continue to skip the remaining code in the current iteration and move on to the next.

The Basic Structure

The basic structure of using continue in a loop in C is straightforward:

AD
C break and continue
C break and continue
C# Continue Statement with Examples - Tutlane
C# Continue Statement with Examples - Tutlane
Conditional statements in c programming
Conditional statements in c programming
```c for (initialization; condition; increment/decrement) { // loop body if (condition_to_skip_iteration) { continue; } // other code to execute for this iteration } ```

This structure allows you to write the condition to skip the current iteration first, followed by the code that should be executed in each iteration. If the condition is met, the loop will skip the rest of the code in the current iteration and move on to the next one.

Example: Skipping Even Numbers

Let's look at an example where we're looping through an array and skipping even numbers:

Interfacing LCD with PIC Microcontroller
Interfacing LCD with PIC Microcontroller
Continue statement in Python language - Code for Java c
Continue statement in Python language - Code for Java c
Java continue Statement (With Examples)
Java continue Statement (With Examples)
```c int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) { if (arr[i] % 2 == 0) { continue; } printf("%d\n", arr[i]); } ```

In this example, the loop will skip over any even numbers in the array, only printing the odd ones.

Example: Early Exit from Function

Another useful scenario is to use continue in combination with a function's *return* statement to exit early from the function:

AD
```c int process(int num) { if (num < 0) { continue; // return immediately if num is negative } // other processing logic... return num * 2; } ```

In this case, if the input number is negative, the function will skip the rest of its processing logic and return immediately with the transformed value.

Loop Control Statements − break and continue
Loop Control Statements − break and continue
C break and continue
C break and continue
Java Jump statements - continue statement
Java Jump statements - continue statement
If statement in C programming with example
If statement in C programming with example
C++ Control Statements
C++ Control Statements
Decision Making Statements in C with Examples
Decision Making Statements in C with Examples

Continue with Labels

In some cases, you might have nested loops and you want to *continue* only the specific loop, not the entire containing loop. In such cases, you can use a label:

```c outer_loop: for (int i = 0; i < 10; i++) { inner_loop: for (int j = 0; j < 10; j++) { if (j % 2 == 0) { continue outer_loop; // Skip the rest of the inner loop and go to the next iteration of outer loop } // other code... } } ```

This structure allows you to skip specific loop iterations instead of exiting the loop entirely.

AD

Example: Complex Nested Loop

Let's look at an example where we're processing a matrix and want to skip certain cells:

Python break and continue (With Examples)
Python break and continue (With Examples)
Java Break vs Continue Explained | Loop Control Statements with Examples | Day 13
Java Break vs Continue Explained | Loop Control Statements with Examples | Day 13
SAS Loops – Understanding Leave and Continue
SAS Loops – Understanding Leave and Continue
C++ If-Else Statement – Clean Syntax Template 🧠
C++ If-Else Statement – Clean Syntax Template 🧠
if statement in Java programming language - Codeforcoding
if statement in Java programming language - Codeforcoding
10: Conditional Statements In C# | If, Else if, Else | C# Tutorial For Beginners | C Sharp Tutorial
10: Conditional Statements In C# | If, Else if, Else | C# Tutorial For Beginners | C Sharp Tutorial
Python Continue Statement Explained | Skip Iteration in Loops
Python Continue Statement Explained | Skip Iteration in Loops
C++ How to Program: A Complete Guide for Beginners
C++ How to Program: A Complete Guide for Beginners
| Pythonista Planet
| Pythonista Planet
What are conditional statements in c language
What are conditional statements in c language
Conditional Statements in C (if, if...else, else if, nested if, switch case in C)
Conditional Statements in C (if, if...else, else if, nested if, switch case in C)
Switch Statement
Switch Statement
Nested if statement in C programming language - Codeforcoding
Nested if statement in C programming language - Codeforcoding
Python Continue Statement | Use, Example
Python Continue Statement | Use, Example
```c int matrix[5][5] = { /*...*/ }; for (int i = 0; i < 5; i++) { row_loop: for (int j = 0; j < 5; j++) { if (matrix[i][j] == 0) { continue row_loop; // Skip the rest of the inner loop and go to the next row } // other processing... } } ```

The key to remember here is to use the label for the loop you want to *continue*, not the containing loop.

Fluent Conclusion

The continue statement offers a clean and efficient way to handle early exits in loops, making your code cleaner and more efficient. With careful placement and use of labels, you can tailor its behavior to fit your specific needs. As you continue to explore C programming, remember the continue statement as a valuable tool in your loop handling toolkit.

AD
AD