In the series of learning C programming, we learned to repeat a set of statements and terminate a repetitive statement using break
. Here in this post, I will explain another program flow control statement i.e. continue
.
We use break keyword to terminate a loop or switch
immediately. However in programming, there exists situations when instead of terminating from loop you need to skip some part of the loop and continue to next iteration.
For example, consider the situation of sending email to 100 employees. In the process of sending email, for each employee you will perform two operation. First, get email address of employee. Afterwards attempt to send email at his address. Suppose an employee does not have email. In such case, instead of terminating entire email process it is better to skip sending mail to that employee and continue to next employee. For such situations in programming we use continue
keyword.
continue
is a jump statement used inside loop. It skips loop body and continues to next iteration. continue
statement on execution immediately transfers program control from loop body to next part of the loop. It works opposite of break
statement. In case of nested loops, it continues to nearest loop.
Syntax of continue
statement
continue;
Flowchart of continue
statement
How to use continue
statement
Rules to use continue
statement in C programming.
- You must use
continue
keyword inside a loop. Use ofcontinue
outside loop will result in compilation error. continue
is not used with switch…case statement.- In case of nested loop,
continue
will continue next iteration of nearest loop. - Do not confuse that
continue
transfer program control to loop condition. It transfer program control to next part of the loop. - Use of
continue
statement without condition is worthless. Hence it is often used with if statements.
Working of continue
statement
continue
statement skips loop body and continues to next iteration. Below are some working examples of continue
statement.
- How
continue
statement works with for loop.
- How
continue
statement works with while loop.
- How
continue
statement works with do…while loop.
Example program to demonstrate continue
statement
Let us write a C program to print all even numbers from 1 to 100. Printing even number is easy however, for this program I will make use of continue
statement.
/**
* C program to print even numbers between 1 to 100
*/
#include <stdio.h>
int main()
{
/* Variable declaration */
int num;
printf("Even numbers between 1 to 100: \n");
for(num=1; num<=100; num++)
{
/*
* If num is odd then
* skip rest loop body and
* continue to next iteration
*/
if(num % 2 == 1)
continue;
/* Print even number */
printf("%d ", num);
}
return 0;
}
Output –
Even numbers between 1 to 100:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Explanation of above program
- Initially
for
loop initialization receives program control. It initializesnum = 1
and transfer program control to loop condition. - Loop condition checks if
num<=100
then transfer program control to loop body otherwise terminate from loop. For num=1 the condition istrue
hence transfers program control to loop body. - Inside loop body I am checking odd condition i.e. if a number is not divisible by 2 then it is odd. Since we only need to print even numbers. Hence, if the above condition is met then skip rest loop body and continue to next iteration.
- For num=1 the condition
(num % 2 == 1)
is true hence,continue
is executed which skips rest of the loop body and continues to next iteration. - For num=2 or any other even number the condition
(num % 2 == 1)
is false hence,printf("%d ", num);
is executed printing the even number.