What is The Use of Continue Statement in C?



Continue statement in C


Just like Break, Continue is also a Keyword in the C programming language.

Before understanding the use of continue statement in C, we will first try to know What is Continue statement, and how does it work? Continue statement is the opposite of the break statement in C. The break is used to terminate the loop. While the continue statement forces to execute the next iteration of the loop.


Continue is a jump statement in C.

If the continue statement is found in the loop body. It skips all statements written after continue and start a new iteration.

Syntax of Continue Statement 

continue;

Flow Chart of Continue Statement 


Continue statement in C

How Continue Statement Works in C?



If the continue statement is found anywhere in the loop body, then the next iteration of the loop will run. Below mention figures clear your doubts on how to continue statement in c work with for loop, while loop or do while loop.

Learn How Continue Statement Works with For Loop?


Continue statement in C

Learn How Continue Statement Works with While Loop?


Continue statement in C

Learn How Continue Statement Works with Do While Loop?


Continue statement in C

Understand Use and Working of Continue Statement  with the help of Example (C Program)

Here, you will learn how to continue statement in C works with for loop, while loop and do-while loop.

C Program Example 1(With For Loop)

#include <stdio.h>
int main()
{
  int i; //Variable Declaration
  for(i=1;i<=10;i++)
  {
    if (i==5)
    {
      continue; //Continue statement in C skips the iteration when i=5
    }
    printf("Value of i = %d\n",i); //Print the value of i
  }
  return 0;
}

Output
Value of i=1
Value of i=2
Value of i=3
Value of i=4
Value of i=6
Value of i=7
Value of i=8
Value of i=9
Value of i=10

C Program Example 2 (With While Loop)

#include <stdio.h>
int main()
{
  int i=0;//Declaration of variable
  while (i<10)
  {
    continue; //Continue statement in C skips the iteration
    printf ("Value of i = %d\n",i);
    i++;
  }
  return 0;
}


Output
Infinite Loop

C Program Example 3 (With Do While Loop)

#include <stdio.h>
int main()
{
  int i=1; //Variable Initialization
  do
  {
    if(i==3)
    {
      i++;
      continue; // Continue Statement in C skips the current iteration (with i=3) and continue next iteration
    }
    printf ("value of i = %d\n",i);
    i++;
  } while(i<7);
  return 0;
}

Output
Value of i = 1
Value of i = 2
Value of i = 4
Value of i = 5
Value of i = 6

C Program to Print a Pattern Using Continue Statement in C

#include <stdio.h>
int main()
{
  int n=5; //Total number of print pattern lines
  int l=1; //Initialize l as line number
  int sn=0; // Number of Star count
  //Loop to print pattern
  for (l=1;l<=n;)
  {
    //If current star count sn is less than the current line number
    if (sn<l)
    {
      printf("*");
      sn++;
      continue;
    }
    // Print a new line
    if(sn==l)
    {
      printf("\n");
      l++;
      sn=0;
    }
  }
}

Output
*
**
***
****
*****

What is the Use of Continue Statement in C?

When you want to stop the iteration but not the loop, then, we use the continue statement in C.

Why Don't We Use the Continue Statement in the Switch Statement in C?

We have used the continue statement in the switch statement in the below mention C program. Let us see what will happen when we use the continue keyword instead of the break in the switch statement.

#include <stdio.h>
int main()
{
  int choice; //Variable Declaration
  printf("Enter the Choice");
  scanf ("%d", &choice);
  //Continue statement in Switch statement
  switch(choice)
  {
  case 1:
    printf ("case 1");
    break;
  case 2:
    printf ("case 2");
    break;
  case 3:
    printf ("case 3");
    continue;
  default:
    printf ("Default Statement");
    break;
  }

  return 0;
}

We got an error when we run the program, and the "error occurs continue statement is not within the loop." The break statement is used to terminate the switch case. It takes control outside the switch case. Because of the continue statement, the control condition returns to the checking statement. Continue statement works only with the loops. Continue statement does not work with the conditional statement in C. That's why continue statement does not work with the switch statement because the switch statement is a conditional statement
What is The Use of Continue Statement in C? What is The Use of Continue Statement in C? Reviewed by Glory on 18:53 Rating: 5

No comments:

Powered by Blogger.