Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/myscript/public_html/classes/classdb.php on line 42
Break Statement - Terminate Switch Case - C++ Control Structures Tutorial

Break statement - Control Structures

How is break statement used in C++?
How to terminate Switch case in Control structures?


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.



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".

C++ Tutorial

Ask Questions

Ask Question