-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlFlow.cpp
More file actions
158 lines (138 loc) · 2.83 KB
/
ControlFlow.cpp
File metadata and controls
158 lines (138 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <random>
using namespace std;
void conditionals()
{
cout << "\n--- Conditional Examples ---\n";
// if-else
int x = rand();
cout << "x = " << x << "\n";
if (x > 0)
{
cout << "x is positive\n";
}
else if (x < 0)
{
cout << "x is negative\n";
}
else
{
cout << "x is zero\n";
}
// switch
int choice = rand() % 2 + 1;
cout << "choice = " << x << "\n";
switch (choice)
{
case 1:
cout << "Choice is 1\n";
break;
case 2:
cout << "Choice is 2\n";
break;
default:
cout << "Choice is something else\n";
break;
}
}
void jumps()
{
cout << "\n--- Jump Statement Examples ---\n";
// goto
int num = rand();
if (num == 3)
goto jumpLabel;
cout << "This line will be skipped.\n";
jumpLabel:
cout << "Jumped here using goto!\n";
// break / continue
for (int i = 0; i < 5; ++i)
{
if (i == 2)
continue; // skip 2
if (i == 4)
break; // stop loop at 4
cout << "i = " << i << "\n";
}
}
int square(int n)
{
// return
return n * n;
}
void functionCalls()
{
cout << "\n--- Function Call Examples ---\n";
// function call
int result = square(5);
cout << "square(5) = " << result << "\n";
}
void loops()
{
cout << "\n--- Loop Examples ---\n";
// while
int i = 0;
while (i < 3)
{
cout << "while loop i = " << i << "\n";
++i;
}
// do-while
int j = 0;
do
{
cout << "do-while loop j = " << j << "\n";
++j;
} while (j < 2);
// for
for (int k = 0; k < 3; ++k)
{
cout << "for loop k = " << k << "\n";
}
// ranged-for
const int arr[] = {10, 20, 30};
for (int value : arr)
{
cout << "ranged-for value = " << value << "\n";
}
}
void halts()
{
cout << "\n--- Halt Examples ---\n";
// std::exit() — terminates the program normally
// std::abort() — terminates abnormally (no cleanup)
// Uncomment one at a time to see behavior
// std::exit(0);
// std::abort();
}
void exceptions()
{
cout << "\n--- Exception Handling Examples ---\n";
// try - catch - throw
try
{
throw runtime_error("Something went wrong!");
}
catch (const exception &e)
{
cout << "Caught exception: " << e.what() << "\n";
}
}
// A struct that runs code when its object is created
struct ControlFlow
{
ControlFlow()
{
cout << "\n"
<< "\n"
<< "ControlFlow\n";
conditionals();
jumps();
functionCalls();
loops();
halts();
exceptions();
}
};
// All global and static objects are constructed before main() begins.
static ControlFlow autoRunInstance;