- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C continue statement
![]() Share with a Friend |
C Programming - C continue statement
C continue Statement
The continue statement in C is used to skip the current iteration of a loop and immediately jump to the next iteration of the loop. It only affects the current iteration, meaning the loop does not terminate entirely but skips the rest of the code in the current iteration and moves to the next one.
Syntax of continue Statement:
C
continue;
- When the continue statement is encountered, the program skips the remaining code inside the loop body for the current iteration and moves to the next iteration, continuing the loop execution.
Where Can continue Be Used?
- The continue statement can be used inside for, while, and do-while loops. It does not work outside these loops.
Example 1: Using continue in a for Loop
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i equals 5
}
printf("%d ", i); // Print values of i
}
return 0;
}
Explanation:
- The for loop runs from 1 to 10, but when i equals 5, the continue statement is encountered.
- The iteration for i = 5 is skipped, and the loop continues with the next value of i.
Output:
1 2 3 4 6 7 8 9 10
Example 2: Using continue in a while Loop
C
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
if (i == 5) {
i++; // Increment before continue to avoid infinite loop
continue; // Skip the iteration when i equals 5
}
printf("%d ", i); // Print values of i
i++;
}
return 0;
}
Explanation:
- The while loop runs until i exceeds 10, but when i equals 5, the continue statement is executed, skipping the current iteration for i = 5.
- The i++ before continue ensures that i is incremented properly, preventing an infinite loop.
Output:
1 2 3 4 6 7 8 9 10
Example 3: Using continue in a do-while Loop
C
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 5) {
i++; // Increment before continue to avoid infinite loop
continue; // Skip the iteration when i equals 5
}
printf("%d ", i); // Print values of i
i++;
} while (i <= 10);
return 0;
}
Explanation:
- The do-while loop works similarly to the while loop, but it ensures that the body is executed at least once before the condition is checked.
- When i equals 5, the continue statement is executed, skipping the current iteration for i = 5.
Output:
1 2 3 4 6 7 8 9 10
Example 4: Using continue with Nested Loops
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
continue; // Skip the iteration when j equals 3
}
printf("%d-%d ", i, j); // Print i and j
}
printf("\n");
}
return 0;
}
Explanation:
- The outer loop runs for i = 1 to 3, and the inner loop runs for j = 1 to 5.
- When j equals 3, the continue statement is executed, which skips the current iteration of the inner loop and continues with the next value of j.
Output:
1-1 1-2 1-4 1-5
2-1 2-2 2-4 2-5
3-1 3-2 3-4 3-5
Key Points About continue Statement:
- Skips current iteration: The continue statement skips the remaining part of the loop body for the current iteration and proceeds to the next iteration.
- Can be used in all loops: It can be used in for, while, and do-while loops.
- Prevents code execution: After encountering continue, the remaining code inside the loop for that iteration is not executed.
- Common use cases:
- Skip specific values or conditions in loops (e.g., skipping even numbers).
- Skip unnecessary calculations or operations for particular cases in loops.
Example 5: Using continue to Skip Even Numbers
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i); // Print odd numbers
}
return 0;
}
Explanation:
- The if condition checks if the number is even (i % 2 == 0). If it is, the continue statement is executed, skipping the print statement for even numbers.
Output:
1 3 5 7 9
Summary of continue Statement:
- The continue statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration of the loop.
- It is useful when you want to skip specific iterations based on a condition while keeping the loop running.
- The continue statement can be used inside for, while, and do-while loops.
