Continue statement is used for continuing a loop when an unexpected condition occurs. Basically this statement helps in passing control to the beginning of the loop.
Illustrates the use of continue statement.
#include<iostream.h> void main() { int n; char ch; ch='y' ; do { cout<<"\n Enter the number"; cin>>n; if(n%5==0) { cout<<"Number "<<n<<" is divisible by 5"; continue; } cout<<"The number "<<n<<" is not divisible by 5"; ch='n'; } while(ch!='n '); }
Input and Output:
Enter the number 10
Number 10 is divisible by 5
Enter the number 20
Number is divisible by 5
Enter the number 3
The number 3 is not divisible by 5
In the above program, initially ch is set to ‘y’. The continue statement helps in transferring control to the beginning of the do .. while loop without executing the statements which follow the continue statement if the number is divisible by 5. Now a new value for the number n is entered and the process is continued as long as the number is divisible by 5. If the number is not divisible by 5, ch is set to ‘n’ and the program gets terminated.