- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Continue Statement in C
The behaviour of continue statement in C is somewhat opposite to the break statement. Instead of forcing the termination of a loop, it forces the next iteration of the loop to take place, skipping the rest of the statements in the current iteration.
What is Continue Statement in C?
The continue statement is used to skip the execution of the rest of the statement within the loop in the current iteration and transfer it to the next loop iteration. It can be used with all the C language loop constructs (while, do while, and for).
Continue Statement Syntax
The continue statement is used as per the following structure −
while (expr){ . . . . . . if (condition) continue; . . . }
Continue Statement Flowchart
The following flowchart represents how continue works −
You must use the continue statement inside a loop. If you use a continue statement outside a loop, then it will result in compilation error. Unlike the break statement, continue is not used with the switch-case statement.
Continue Statement with Nested Loops
In case of nested loops, continue will continue the next iteration of the nearest loop. The continue statement is often used with if statements.
Continue Statement Examples
Example: Continue Statement with While Loop
In this program the loop generates 1 to 10 values of the variable "i". Whenever it is an even number, the next iteration starts, skipping the printf statement. Only the odd numbers are printed.
#include <stdio.h> int main(){ int i = 0; while (i < 10){ i++; if(i%2 == 0) continue; printf("i: %d\n", i); } }
Output
i: 1 i: 3 i: 5 i: 7 i: 9
Example: Continue Statement with For Loop
The following program filters out all the vowels in a string −
#include <stdio.h> #include <string.h> int main () { char string[] = "Welcome to TutorialsPoint C Tutorial"; int len = strlen(string); int i; printf("Given string: %s\n", string); printf("after removing the vowels\n"); for (i=0; i<len; i++){ if (string[i]=='a' || string[i]=='e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u') continue; printf("%c", string[i]); } return 0; }
Output
Run the code and check its output −
Given string: Welcome to TutorialsPoint C Tutorial after removing the vowels Wlcm t TtrlsPnt C Ttrl
Example: Continue Statement with Nested Loops
If a continue statement appears inside an inner loop, the program control jumps to the beginning of the corresponding loop.
In the example below, there are three for loops one inside the other. These loops are controlled by the variables i, j, and k respectively. The innermost loop skips the printf statement if k is equal to either i or j, and goes to its next value of k. The second j loop executes the continue when it equals i. As a result, all the unique combinations of three digits 1, 2 and 3 are displayed.
#include <stdio.h> int main (){ int i, j, k; for(i = 1; i <= 3; i++){ for(j = 1; j <= 3; j++){ if (i == j) continue; for (k=1; k <= 3; k++){ if (k == j || k == i) continue; printf("%d %d %d \n", i,j,k); } } } return 0; }
Output
Run the code and check its output −
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Example: Removing Spaces Between Words in a String
The following code detects the blankspaces between the words in a string, and prints each word on a different line.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main(){ char string[] = "Welcome to TutorialsPoint C Tutorial"; int len = strlen(string); int i; printf("Given string: %s\n", string); for (i = 0; i < len; i++){ if (string[i] == ' '){ printf("\n"); continue; } printf("%c", string[i]); } return 0; }
Output
On executing this code, you will get the following output −
Given string: Welcome to TutorialsPoint C Tutorial Welcome to TutorialsPoint C Tutorial
Example: Finding Prime Factors of a Number
One of the cases where the continue statement proves very effective is in the problem of writing a program to find prime factors of a given number.
The algorithm of this program works like this −
The given number is successively divided by numbers starting with 2. If the number is divisible, the given number is reduced to the division, and the resultant number is checked for divisibility with 2 until it is no longer divisible.
If not by 2, the process is repeated for all the odd numbers starting with 3. The loop runs while the given number reduces to 1.
Here’s the program to find the prime factors −
#include <stdio.h> int main (){ int n = 64; int i, m = 2; printf("Prime factors of %d: \n", n); while (n > 1){ if (n % m == 0){ n = n/m; printf("%d ", m); continue; } if (m == 2) m++; else m = m+2; } return 0; }
Output
Here, the given number is 64. So, when you run this code, it will produce the following output −
Prime factors of 64: 2 2 2 2 2 2
Change the number to 45 and then 90. Run the code again. Now you will get the following outputs −
Prime factors of 45: 3 3 5 Prime factors of 90: 2 3 3 5