break

Terminates execution of the nearest enclosing do, for, switch, or while statement in which it appears.

 

#include <iostream>
using namespace std ;

int main () {

	for (int myVar = 0 ; myVar < 7 ; myVar++) {

		cout << "myVar is now: " << myVar << endl ;

		if (myVar == 3)
			break;  //the loop will exit when myVar equates to 3
	}
	return 0;
}

Compile & Run:

myVar is now: 0
myVar is now: 1
myVar is now: 2
myVar is now: 3

Leave a Reply