11#include < iostream>
2- #include < random>
3- using namespace std ;
2+ #include " ExampleRegistry.h"
43
4+ namespace {
55void conditionals () {
6- cout << " \n --- Conditional Examples ---\n " ;
6+ std:: cout << " \n --- Conditional Examples ---\n " ;
77 // if-else
88 int x = rand ();
9- cout << " x = " << x << " \n " ;
9+ std:: cout << " x = " << x << " \n " ;
1010 if (x > 0 ) {
11- cout << " x is positive\n " ;
11+ std:: cout << " x is positive\n " ;
1212 } else if (x < 0 ) {
13- cout << " x is negative\n " ;
13+ std:: cout << " x is negative\n " ;
1414 } else {
15- cout << " x is zero\n " ;
15+ std:: cout << " x is zero\n " ;
1616 }
1717
1818 // switch
1919 int choice = rand () % 2 + 1 ;
20- cout << " choice = " << x << " \n " ;
20+ std:: cout << " choice = " << x << " \n " ;
2121 switch (choice) {
2222 case 1 :
23- cout << " Choice is 1\n " ;
23+ std:: cout << " Choice is 1\n " ;
2424 break ;
2525 case 2 :
26- cout << " Choice is 2\n " ;
26+ std:: cout << " Choice is 2\n " ;
2727 break ;
2828 default :
29- cout << " Choice is something else\n " ;
29+ std:: cout << " Choice is something else\n " ;
3030 break ;
3131 }
3232}
3333
3434void jumps () {
35- cout << " \n --- Jump Statement Examples ---\n " ;
35+ std:: cout << " \n --- Jump Statement Examples ---\n " ;
3636
3737 // goto
3838 int num = rand ();
3939 if (num == 3 )
4040 goto jumpLabel;
41- cout << " This line will be skipped.\n " ;
41+ std:: cout << " This line will be skipped.\n " ;
4242
4343jumpLabel:
44- cout << " Jumped here using goto!\n " ;
44+ std:: cout << " Jumped here using goto!\n " ;
4545
4646 // break / continue
4747 for (int i = 0 ; i < 5 ; ++i) {
4848 if (i == 2 )
4949 continue ; // skip 2
5050 if (i == 4 )
5151 break ; // stop loop at 4
52- cout << " i = " << i << " \n " ;
52+ std:: cout << " i = " << i << " \n " ;
5353 }
5454}
5555
@@ -59,43 +59,43 @@ int square(int n) {
5959}
6060
6161void functionCalls () {
62- cout << " \n --- Function Call Examples ---\n " ;
62+ std:: cout << " \n --- Function Call Examples ---\n " ;
6363 // function call
6464 int result = square (5 );
65- cout << " square(5) = " << result << " \n " ;
65+ std:: cout << " square(5) = " << result << " \n " ;
6666}
6767
6868void loops () {
69- cout << " \n --- Loop Examples ---\n " ;
69+ std:: cout << " \n --- Loop Examples ---\n " ;
7070
7171 // while
7272 int i = 0 ;
7373 while (i < 3 ) {
74- cout << " while loop i = " << i << " \n " ;
74+ std:: cout << " while loop i = " << i << " \n " ;
7575 ++i;
7676 }
7777
7878 // do-while
7979 int j = 0 ;
8080 do {
81- cout << " do-while loop j = " << j << " \n " ;
81+ std:: cout << " do-while loop j = " << j << " \n " ;
8282 ++j;
8383 } while (j < 2 );
8484
8585 // for(initialization; condition; update)
8686 for (int k = 0 ; k < 3 ; ++k) {
87- cout << " for loop k = " << k << " \n " ;
87+ std:: cout << " for loop k = " << k << " \n " ;
8888 }
8989
9090 // ranged-for
9191 const int arr[] = {10 , 20 , 30 };
9292 for (int value : arr) {
93- cout << " ranged-for value = " << value << " \n " ;
93+ std:: cout << " ranged-for value = " << value << " \n " ;
9494 }
9595}
9696
9797void halts () {
98- cout << " \n --- Halt Examples ---\n " ;
98+ std:: cout << " \n --- Halt Examples ---\n " ;
9999
100100 // std::exit() — terminates the program normally
101101 // std::abort() — terminates abnormally (no cleanup)
@@ -106,17 +106,16 @@ void halts() {
106106}
107107
108108void exceptions () {
109- cout << " \n --- Exception Handling Examples ---\n " ;
109+ std:: cout << " \n --- Exception Handling Examples ---\n " ;
110110
111111 // try - catch - throw
112112 try {
113- throw runtime_error (" Something went wrong!" );
114- } catch (const exception& e) {
115- cout << " Caught exception: " << e.what () << " \n " ;
113+ throw std:: runtime_error (" Something went wrong!" );
114+ } catch (const std:: exception& e) {
115+ std:: cout << " Caught exception: " << e.what () << " \n " ;
116116 }
117117}
118-
119- #include " ExampleRegistry.h"
118+ } // namespace
120119
121120class ControlFlow : public IExample {
122121 public:
0 commit comments