Answer to Question #297587 in C++ for Aniket

Question #297587

When will the ‘break’ and ‘continue’ statements be used in programme? Mention the scenario and write the programme to demonstrate this.


1
Expert's answer
2022-02-14T02:17:50-0500
#include<iostream>

using namespace std;

/*Break and continue are known as jump statements because they are generally used
to change or manipulate the regular flow of the program, loops, etc. when a particular
condition is met.
The break statement is used to terminate the execution of the current loop.
Whenever there is a need to end the loop, you need to add the break statement.
Once the break statement is met, all the iterations are stopped, and the control
is shifted outside the loop.
The continue statement is used to move to the next iteration, it is also used for
termination, but unlike break, it is not used to terminate the entire execution of
the loop but only the current iteration of the loop.*/

int main()
{
	//Make a program, that ask user to enter a value and count sum
	//of all even numbers from 0 to value
	int value;
	cout<<"Please, enter a value: "; 
	cin>>value;
	double sum=0;
	for(int i=value;;i--)
	{
		if(i==0)
			break;//break from loop
		else if(i%2==1)
			continue;//jump to the next iteration
		else
			sum+=i;
	}
	cout<<"Summ of all even numbers from 0 to "<<value<<" is "<<sum;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog