Continue is also a loop control statement just like the break statement. continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
As the name suggest the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and next iteration of the loop will begin.
Consider the situation when you need to write a program which prints number from 1 to 10 and but not 6. It is specified that you have to do this using loop and only one loop is allowed to use.
Here comes the usage of continue statement. What we can do here is we can run a loop from 1 to 10 and every time we have to compare the value of iterator with 6. If it is equals to 6 we will use the continue statement to continue to next iteration without printing anything otherwise we will print the value.
Below is the implementation of above idea:
1 2 3 4 5 7 8 9 10
The continue statement can be used with any other loop also like while or do while in the similar way as it is used with for loop above.
Exercise Problem :
Given a number n, print triangular pattern. We are allowed to use only one loop.
Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- vector::begin() and vector::end() in C++ STL
- Difference between while(1) and while(0) in C language
- goto statement in C/C++
- Break Statement in C/C++
- Using range in switch case in C/C++
- C++ Classes and Objects
- Command line arguments in C/C++
- Loops in C++
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Different methods to reverse a string in C/C++
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- Functions in C/C++
- Arrays in C/C++
- Interesting facts about switch statement in C
- Storage for Strings in C