C continue

The continue statement in C language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.

The continue statement can be used with looping statements like while, do-while and for.When we use continue statement with while and do-while statements the execution control directly jumps to the condition. When we use continue statement with for statement the execution control directly jumps to the modification portion (increment/decrement/any modification) of the for loop.

Syntax 

//loop statement

continue;

//some lines of the code which is to be skipped

 


Example 

If the user enters a negative number, it's not added to the result.

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

   for (i = 1; i <= 10; i++) {
      printf("\n Enter a number%d: ", i);
      scanf("%d", &number);

      if (number < 0) {
         continue;
      }

      sum += number; // sum = sum + number;
   }

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

   return 0;
   getch();
}

 Output


 

 



No comments:

Post a Comment