by Dinesh Thakur Category: Control Structures

The continue statement is another loop interruption statement provided in the C language. It interrupts only the current iteration of the loop as opposed to the break statement which interrupts the execution of the entire loop. The format of the continue statement is as follows:

continue;

When this statement is executed, control is passed to the next iteration of the loop in which it appears, bypassing any remaining statements in that loop. In case of while and do ... while, the control is passed to the terminating condition, whereas in the for loop, the update expression is evaluated before the control is passed to the final expression.

Example: Using the continue statement within loops

Addition of positive numbers in a given list

Consider the program segment given below that accepts n integer numbers from the keyboard and determines the sum of only positive numbers in this list.

sum = 0;

for (i = 0; i < n; i++)

   {

       scanf("%d", &num);

       if (num <= 0)

           continue;

       sum += num;

   }

The continue statement causes control to be transferred to update expression ( i++)and then to the final expression, i. e., i < n, skipping the execution of the statement that updates the value of sum. Note that this code can also be rewritten without the continue statement as shown below.

sum = 0;

for (i = 0; i < n; i++)

 {

     scanf("%d", &num);

     if (num > 0)

         sum += num;

 }

 

Illustrates the use of continue statement

#include <stdio.h>

void main()
{
  int a = 11 , b = 50;
  clrscr();
  while(1)
  {
   if (b% a == 0)
     goto Finish;
     else
     a++;
     continue;
  }
 Finish:
 printf("a = %d \t b=%d\n" , a, b);
}

 

The expected output obtained on running the program is as given below.

       continue Statement in C Language



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article