The continue statement in C Language

Let us understand the continue statement in C programming language.

continue statement works similar to break statement in C. Instead of terminating the loop statement, it will force next iteration of the loop. continue statement can be used with for loop, while loop and do-while loop.

Sometimes there arises a situation where we don’t want to execute the certain statements within the loop but we want to continue the execution of the loop till the condition is satisfied. This can be done using continue statement.

Consider the below example,

for(i=0;i<10;i++)
{
    /*statement1*/
    /*statement2*/
    /*statement3*/
    /*statement4*/
}

Let us suppose, when the value of i equals to 5 we want to skip the execution of statement3 and statement4. Then, we can use the continue statement in C to perform the above task as follows.

for(i=0;i<10;i++)
{
    /*statement1*/
    /*statement2*/
    
    if(i ==5)
    	continue;
    
    /*statement3*/
    /*statement4*/
}

Here, when value of i equals to 5, statement3 and statement4 are skipped and control gets transferred to the increment part of for loop and for loop is executed as normal.

You may also like...

25 Responses

  1. Anonymous says:

    eloborate it clearly by using example plsss… so that it wl b perctly clear

  2. Anonymous says:

    what does for(i=0;i<10;i++ )
    mean

    • Jason Yutani says:

      for(i = 0; i < 10; i++) is kinda like a loop. It tests the conditions inside the parentheses. In english "The variable i is equal to 0, if i is greater less then 10, increment i"

  3. Anonymous says:

    What is the statement
    “i++”? Is it i being positive?

    And I agree with my grammatically challenged friend here, your other chapters have about two or three examples of all the possibilities. Or is that all you can do?

  4. LearnCOnline says:

    If value of i = 0 then after execution of “i++”, value of i will become 1. Hope your doubt is clear. thanks

  5. Anonymous says:

    if i want to skip it at multiple conditions suppose when i=5,6,7 then it will skip the statement 5,6. How the prog is written

  6. LearnCOnline says:

    If you like to skip when i=5,6,7 then you can write the below code:
    for(i=0;i<10;i++){
    /*statement1*/
    /*statement2*/
    if(i == 5 || i == 6 || i == 7)
    continue;
    /*statement3*/
    /*statement4*/
    }

    • Vishal13 says:

      #include
      void main()
      {
      int i;
      for(i=0;i<10;i++)
      {
      if(i==5||i==6||i==7)
      continue;
      printf("\nThe number is:%d",i);
      }
      }

  7. Kandarp Patel says:

    Good enough for me. Started today from first post of this series.Never made any sense for me back in college days but, now certainly is. I am a T-SQL developer and now my goal is C, Objective- C and cocoa. LearnCOnline, you’re doing great job. Thank you!

  8. Anonymous says:

    why do we need continue statement?

  9. LearnCOnline says:

    Continue statement is needed if we want to skip the further part of the code within a loop and go back to evaluate the loop condition. Hope its clear.
    If no then kindly reply.

    • Rumon says:

      those sense nothing!! please give an example without /*statement3*/ type bullshits. give an example and show the output, then that will make some sorts of senses. thanks.

  10. Michael Cook says:

    Can it be used to only skip part of the remaining statements?
    E.G skip statement3 but still action statement4?

  11. Manoj Rathod says:

    @michael cook :-
    yes

  12. if we want statement3 not get executed wheni=5,but want statement 4 to be executed.then how continue statement is helpful

  13. Anonymous says:

    can any one say the difference between ++i and i++.pls give me a fast reply 🙂

  14. if we want statement4 not get executed wheni=5, but want statement
    3 be excuted

  15. Anonymous says:

    how we can replace continue statement with something else?
    is it possible?

  16. Vishal says:

    for(i=0;i<10;i++) is the for loop statement in the C programming language. It is used when the programmer want to repeat the particular block of statement repeat and repeat again while condition(i<10) get satisfied.
    Here,
    i=0, is the initialization of the counter in the for loop
    i<10 is the test condition for the counter in the for loop to continue loop till the condition return false.
    i++ is the increment statement. This statement increase the value of i each time and then condition is checked.
    I have described this for loop as follows:=

    #include
    void main()
    {
    int i;
    for(i=0;i<5;i++)
    {
    printf("\nThe number is:%d",i);
    }
    }
    Description of the program:=
    In the above program, I initialized counter as i=0 in for loop, then condition is checked (i<5)here,i=0 then condition is satisfied then block of code to be executed that means i is print as 0, then control goes back in for loop at i++ to increment the value of i=1, now value of i is 1, then condition is checked, here now again condition get satisfied then printed value of i is 2, this will continue till the value of i gets to 5.
    If the value of i is 5 then condition is checked as (i<5) that means(5<5) this is not true condition then control goes out of for loop and the execution will stop or terminated.

    Output of Above program is:
    The Number is: 0
    The Number is: 1
    The Number is: 2
    The Number is: 3
    The Number is: 4

  17. Vishal13 says:

    /*
    program to print
    7 8 9
    7 8
    7
    */

    #include
    void main()
    {
    int i,j,n;
    for(i=1;i<=3;i++)
    {
    n=7;
    for(j=i;j<=3;j++)
    {
    printf("\n\n\t%d",n);
    n++;
    }
    }
    }

  18. Vishal13 says:

    give me difference between the ++i and i++.

    • Prashant N. says:

      The ++ or — operator can be used in two ways:
      Example:
      ++m and m++

      These are two different expressions. The first expression immediately increments the value of m by 1. For example, the statements:
      int t, m = 1;
      t = ++m;

      will cause the value of m to incremented first, and then this value is assigned to t, resulting in both having the same value.

      On the other hand, the statements:
      int t, m = 1;
      t = m++;

      will first evaluate the value of m (which is 1), resulting in 1 being assigned to t and then the value of m being incremented to 2.

      The same case applies to decrement operator (--) too

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet