Break statement - Control Structures
How is break statement used in C++?
How to terminate Switch case in Control structures?
Explanation
Break statement is commonly used with the "switch" statement to terminate a case. But this statement can also be used to terminate from a loop abruptly.
Syntax:
break;
Example :
#include <iostream.h> int main() { int i; for ( i=0 ; i<5 ; i++ ) { cout << "Counter value is::" << i << '\n'; if (i ==3) { break; } } cout << "Exited loop using break statement." << '\n'; }
|
Result :
Counter value is::0
Counter value is::1
Counter value is::2
Counter value is::3
Exited Loop using the break statement.
In the above example, break statement is used to exit the loop when the value of i becomes "3".