4.5.Break, Continue And Go to label #

  • Break Statement

Break statement used for end of loop’s process or switch statement.
Syntax 1:

for( initialization ; condition ; iteration)
{
	statement;
        if(condition)
	{
		break;
	}
}

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
	for(int i=1;i<=1000;i++)
	{
		cout<<"Hello "<<i<<endl;
		if(i==10)
			break;
	}
	getch();
}
  • Continue statement

Continue statement used for skip statement in process of loop.

Syntax:
for( initialization ; condition ; iteration)
{
	if(condition)
	{
		continue;
	}
	statement;	
}

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
	for(int i=1;i<=100;i++)
	{
		if(i==5)
			continue;
		cout<<i<<"\t";
	}
	getch();
}
  • Go to label

Go to statement used for you that want to run code again on the label’s point that you want to run again.
Syntax:

label_name:
statement;
if(condition)
         geto label_name;
statement;

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
	double v1,v2,sum;
	top:
	clrscr();
	cout<<"Enter V1: "; cin>>v1;
	cout<<"Enter V2: "; cin>>v2;
	sum=v1+v2;
	cout<<"Result Of Sum: "<<sum<<endl;
	cout<<"Do you wish to run this program again? Press 'y' to continue!";
	if(getch()=='y')
		goto top;
        getch();
}
Suggest Edit