C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

continue statement in C

The continue statement skips the current iteration of the loop and continues with the next iteration. Its syntax is:

continue;

The continue statement is almost always used with the if...else statement.

Example : continue statement

// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, it's not added to the result

#include <stdio.h>
int main() {
int i, num, sum = 0;

for (i = 1; i <= 10; ++i) {
printf("Enter Number %d : ", i);
scanf("%d", &num);

if (num < 0) {
continue;
}

sum += num; // sum = sum + num;
}

printf("Sum = %d", sum);

return 0;
}

 

OUTPUT:

Enter Number 1 : 1
Enter Number 2 : 2
Enter Number 3 : 3
Enter Number 4 : 4
Enter Number 5 : 5
Enter Number 6 : -1
Enter Number 7 : -1
Enter Number 8 : -1
Enter Number 9 : -1
Enter Number 10 : 0


Sum = 15

In this program, when the user enters a positive number, the sum is calculated using sum += num; statement.

When the user enters a negative number, the continue statement is executed and it skips the negative number from the calculation.