Break Example
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
/* Break can be used to exit from a loop */
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
cout<< "loop breaks" << endl;
break;
} else
{
cout<< "Value = " << i << endl;
}
}
/* Also used to exit from switch case */
int a = 10;
switch (a) {
case 5:
cout<< "Value of a is 5" << endl;
break;
case 10:
cout<< "Value of a is 10" << endl;
break;
case 15:
cout<< "Value of a is 15" << endl;
break;
}
return 0;
}
Output:
Value = 0
Value = 1
Value = 2
loop breaks
Value of a is 10