Skip to content

Commit c4661f4

Browse files
authored
Merge pull request #3 from urboob21/dev_branch
Add core/basic/controlflows
2 parents 6738734 + fc59387 commit c4661f4

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ set(APP_SOURCES
7878
"src/DeleteMe.cpp"
7979
"src/core/basics/InitializeVariable.cpp"
8080
"src/core/basics/Operations.cpp"
81+
"src/core/basics/ControlFlow.cpp"
8182
)
8283

8384
# Test files

src/core/basics/ControlFlow.cpp

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

0 commit comments

Comments
 (0)