LECTURE 9: CONTINUE
hints
• Syntax
• Flowchat
• How to use
• Working
• example
• We use break keyword to terminate a loop
or switch immediately.
• However in programming, there are 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 their 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.
Cont..
• 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
• continue;
Flowchat of continue statement
Rules to use continue statement in C
programming.
– You must use continue keyword inside a loop.
• Use of continue 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 1
• 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….100
Example:-
void main() {
int n;
for(n=2; n<=9; n++) {
if(n==4)
continue;
printf(“%d”, n);
Output: 2 3 5 6 7 8 9 out of loop
}
}
Printf(“out of loop”);
}
As an example, let's consider the
following program
main( )
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "\n%d %d\n", i, j ) ;
}
}
}
• The output of the above program would be...
12
21
Note
• when the value of i equals that of j, the
continue statement takes the control to the
for loop (inner) bypassing rest of the
statements pending execution in the for loop
(inner).
End of the lecture