continue is important keyword used to alter the flow of a program. 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
The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.
For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests.
Flowchart of continue statement
How to use continue statement
Rules to use continue statement in C programming.
continue statement skips loop body and continues to next iteration. Below are some working examples of continue statement.
continue statement with for loop
continue statement with while loop
continue statement with do-while loop
Example program to demonstrate continue statement
// Program to calculate the sum of a maximum of 10 numbers
// Negative numbers are skipped from the calculation
# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
if(number < 0.0)
{
continue;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a n1: 1.1
Enter a n2: 2.2
Enter a n3: 5.5
Enter a n4: 4.4
Enter a n5: -3.4
Enter a n6: -45.5
Enter a n7: 34.5
Enter a n8: -4.2
Enter a n9: -1000
Enter a n10: 12
Sum = 59.70