How to Use Continue Statement Using C++ Language in DEV C++

Continue statement:

This is very interesting statement.
As in the switch statement i discuss Break statement forces the termination of the loop, while continue statement causes the next iteration of the loop.

Syntax:

continue;

Continue statement behavior on for loop:
The behavior of continue statement on for loop is on the conditional statement and increments.

Continue statement behavior of while and do while loop:
The behavior of continue statement on while and do while loop is on the conditional statements.

Lets take an example on for loop.I will show you how continue statement behaves. I am giving two examples one without continue statement and other with continue statement using for loop.

Program code without using continue statement:
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0;

for(int i =x;i<20;i++)
{
x= i + 1;
cout<< "value of x  =  "<< x <<endl;
}
cout<<endl;
cout<<"end of for loop execution"<<endl;

}


I define one variable which is of integer type and assigns a value 0 to it. In the for loop i define anther integer variable and assigns the value of variable x , which i defined earlier.
This loop will execute 20 times and gives you output according to statement defined inside the for loop that is x = i +1 ;
After loop execution , flow of program jumps to statement after for loop , which will execute.

Output: After compiling and executing




 Program code with continue statement:


Now here i am using continue statement , now you can clearly see that when for loop going for first iteration , it will execute the statement x = i +1; ,  but after that it will go for the execution of continue statement , after that it will not go for next statement after continue statement , rather it will go for the next iteration of the loop. This process will continue till condition in the for loop becomes wrong and flow of program jumps to statements after for loop.Now you can see the output given below.


Output: After compiling and executing




Keep practicing on it.


Exercises:
1.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
continue;
x=y;
cout<< "value of x  =  "<< x <<endl;
}
cout<<endl;
cout<<"end of for loop execution"<<endl;

}


2.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
continue;
x=y;
cout<< "value of x  =  "<< x <<endl;
}

cout<<endl;
x = y;
cout<<"end of for loop execution"<<endl;

}


3.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
continue;
x=y;
cout<< "value of x  =  "<< x <<endl;
}

cout<<endl;
x = y;
continue;
cout<<"end of for loop execution"<<endl;

}


4.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
continue;

x= i + y + 1;

cout<< "value of x  =  "<< x <<endl;
}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}


5.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;

cout<< "value of x  =  "<< x <<endl;

continue;
}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}


6.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;

cout<< "value of x  =  "<< x <<endl;

continue;

x++;
}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}


7.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
    x++;
cout<< "value of x  =  "<< x <<endl;

continue;

}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}


8.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
    x++;
    x--;
    x--;
cout<< "value of x  =  "<< x <<endl;

continue;

}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}


9.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++)
{
x= i + y + 1;
    x++;
    x--;
    x--;
    
    x = 0;
cout<< "value of x  =  "<< x <<endl;

continue;

}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}

10.What is the output of following program ? 

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 0 , y = 5;

for(int i =x;i<20;i++);
{
x= i + y + 1;
    x++;
    x--;
    x--;
    
    x = 0;
cout<< "value of x  =  "<< x <<endl;

continue;

}

cout<<endl;
cout<<"end of for loop execution"<<endl;

}

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.