Monday, July 15, 2013

The continue Statement in C

In C, along with the control Flow statements, there are other two statements which also controls the flow. Among them one is continue statement while the other is break statement.

While the break statement gets the flow out of a loop, continue statement makes the loop to turn to the next iteration. It doesn't break the entire loop, but just breaks the current iteration and makes the loop to move on to the next iteration.

Syntax:
continue;
In C, this continue statement is useful for making the loop to move to the next iteration. Consider the following example, to understand the continue statement.

Example:

  1. #include<stdio.h>
  2. main()
  3. {
  4. int a;
  5. for(a=0;a<5;a++)
  6. {
  7. if(a==3)
  8. continue;
  9. printf("%d", a);
  10. }
  11. return 0;
If you execute the above example, the output you get is like this:
  • 0
  • 1
  • 2
  • 4
If the value of a is 3, then we have said to continue. So instead of executing the statements below it, it moves to the next iteration.

If we use break instead of continue, the execution stops and gets out of the loop.
That is the major difference between these two statements.