├── .github └── FUNDING.yml ├── README.md └── _config.yml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jdevstatic 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Core Concepts of Computer Programming 2 | 3 | *`updated July 31, 2024`* 4 | 5 | [![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fjdevstatic%2Fprogramming-core-concepts&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=PAGE+VIEWS&edge_flat=false)](https://hits.seeyoufarm.com) 6 | 7 | TOC 8 | 1. [Intro](#intro) 9 | 2. [Input / Output](#input-and-output) 10 | 1. [Hello World Program](#hello-world-program) 11 | 3. [Variables & Data Types](#variables-and-data-types) 12 | 1. [Sample Program for Variables and Data Types](#sample-program-for-variables-and-data-types) 13 | 4. [Operators](#operators) 14 | 1. [Assignment Operators Sample Program](#assignment-operators-sample-program) 15 | 2. [Arithmetic Operators Sample Program](#arithmetic-operators-sample-program) 16 | 3. [Comparison Operators Sample Program](#comparison-operators-sample-program) 17 | 5. [Conditionals](#conditionals) 18 | 1. [Sample Program Using IF](#sample-program-using-if) 19 | 2. [Sample Program Using IF/ELSE](#sample-program-using-if-else) 20 | 6. [Loops](#loops) 21 | 1. [Sample Program Using Loops](#sample-program-using-loops) 22 | 7. [Functions](#functions) 23 | 1. [Sample Program Creating And Using Functions](#sample-program-creating-and-using-functions) 24 | 8. [More Of My Content](#more-of-my-content) 25 | 26 | ## Intro 27 | Of all the inventions in the field of electronics like the radio and 28 | telephone, the programmable computer was the most significant one. It 29 | changed the world forever. 30 | 31 | If a certain device can be programmed the way we want it to behave, 32 | then we can simply instruct it to do things for us. That's very 33 | practical, isn't it? 34 | 35 | After programmable computers, the next big question was how humans 36 | would program them. They began developing programming languages and 37 | the 1970s was a very remarkable period: the C language was invented. 38 | 39 | After this success, great potential was unleashed, and we are now 40 | seeing what a programmable computer can do: the era of information, 41 | real-time data, almost realistic 3D games, artificial intelligence, 42 | etc. 43 | 44 | Learning computer science today, unlike in the past, involves many 45 | specialized fields, even in software development alone. 46 | 47 | And if you are a beginner, you might be discouraged from all the things 48 | you need to learn but don't worry: **you don't need to learn them all**. 49 | What you need to learn first is understanding how computers work and 50 | the fundamentals of computer programming that all computer programming 51 | languages have in common. 52 | 53 | And since almost every programming language nowadays directly and 54 | indirectly inherits concepts from the C language, we'll be using it to 55 | demonstrate the core concepts of programming. 56 | 57 | *Take note: we don't want to discuss every detail of the code in terms 58 | of the C language, rather we just want to get the core concepts of 59 | computer programming that are common in different programming 60 | languages.* 61 | 62 | ## Input And Output 63 | As was mentioned, a programmable computer is very 64 | important, but the way you interact with the computer 65 | is through input and output: you, as the user, 66 | will provide input, the computer will process it 67 | (the way it is processed is also programmed), 68 | then the computer will output something. 69 | The very first demonstration is the "Hello World" 70 | program. The programmer will tell the computer 71 | to simply display the message on the screen. 72 | 73 | But of course, complex programs are not as straightforward 74 | as that. Typical programs like binary 75 | to human language (and vice versa) 76 | takes a lot of processing, 77 | an operating system to recognize computer applications, 78 | a business collaborative tool with many features 79 | to manage data, a 3D game that is almost realistic, 80 | an Artificial Intelligence program that can simulate humans 81 | in the virtual world! 82 | 83 | But of all the things programmers can do, the 84 | computer only processes input as binary data 85 | (0s and 1s), and it should not be interpreted literally. 86 | It's just a representation of something a computer 87 | can recognize: the absence or presence of an electric pulse. 88 | Compare this with any drawing 89 | that can be accomplished just 90 | by simply using a dot-and-no-dot pattern! 91 | It's the same thing for the computer. 92 | 93 | ### Hello World Program 94 | 95 | ```c 96 | #include 97 | 98 | int main() { 99 | 100 | printf("Hello World!\n"); 101 | printf("Life is a blessing!\n"); 102 | 103 | return 0; 104 | 105 | } 106 | ``` 107 | 108 | In this small program, the `#include` tells the computer to include a 109 | certain source which is needed because it contains the predefined 110 | command `printf`. `printf` simply tells the computer to display the 111 | text provided by the programmer. 112 | 113 | As simple as that, you now have the complete idea of what a 114 | programmable computer is all about. It's you telling the computer to 115 | do things it can handle in terms of binary data. 116 | 117 | ## Variables And Data Types 118 | 119 | In computer programming, a **variable** is just like a container to 120 | store digital data. A **data type** is the way you tell the computer 121 | how that data will be interpreted. Should the computer interpret that 122 | as letters? or numbers? or words? or simply raw binary data? The 123 | computer does not know that, you must tell it exactly. 124 | 125 | In computer programming, both these things will enable you to store 126 | data and tell the computer what kind of data it is. When you store, 127 | you want to retrieve it later as needed. Remember also, computer's 128 | memory is different from storage. Think of it as the short-term memory 129 | and the storage (the hard disk) as the long-term memory. But during 130 | the runtime of a program you are developing, you refer to memory as 131 | `storage`. The computer's memory has full access to the CPU while the 132 | disk does not have that access. In order to read contents from the 133 | hard disk, a request should be made. Hence, in programming, you are 134 | using the memory first, not the disk. There is current development to 135 | combine the two, but it is still ongoing. 136 | 137 | Take note, the details of memory, hard disk, and CPU are quite complex, 138 | but the mentioned details above will serve as the starting point for 139 | full comprehension. 140 | 141 | ### Sample Program for Variables and Data Types 142 | ```c 143 | #include 144 | 145 | int main() { 146 | 147 | int i = 1; 148 | char myletter[] = "myletter"; 149 | float x = 1.23; 150 | 151 | printf("int i: %d\n", i); 152 | printf("char myletter[]: %s\n", myletter); 153 | printf("float x: %f\n", x); 154 | 155 | return 0; 156 | } 157 | ``` 158 | 159 | In this simple program, we declare and assign 160 | three variables with different data types: 161 | 1. an integer variable `i` that contains the numeric value `1` 162 | 2. a character array `myletter[]` that contains the string `"myletter"` 163 | 3. a float variable `x` that contains the value `1.23` 164 | 165 | After that, the program instructs the computer to display these values. 166 | 167 | ## Operators 168 | In programming, just like in mathematics, there are operators. The most 169 | common in computer programming are assignment, arithmetic, and 170 | comparison operators. 171 | 172 | ### Assignment Operators Sample Program 173 | ```c 174 | #include 175 | 176 | int main() { 177 | int c; 178 | 179 | c = 10; 180 | printf("c = %d\n", c); 181 | c = 15; 182 | printf("c = %d\n", c); 183 | c = 3; 184 | printf("c = %d\n", c); 185 | 186 | return 0; 187 | } 188 | ``` 189 | 190 | The most common assignment operator is `=`. It should not be confused 191 | with the double `==` comparison operator. 192 | 193 | In this example, the `=` operator is simply assigning a value to a 194 | variable. The result is: 195 | 196 | ``` 197 | c = 10 198 | c = 15 199 | c = 3 200 | ``` 201 | 202 | Variable `c` contains a value at a specific time. It was changed in the 203 | program three times. 204 | 205 | ### Arithmetic Operators Sample Program 206 | ```c 207 | #include 208 | 209 | int main() { 210 | int a = 10; 211 | int b = 5; 212 | int result; 213 | 214 | printf("where a = %d & b = %d\n", a, b); 215 | 216 | result = a + b; 217 | printf("a + b = %d\n", result); 218 | result = a - b; 219 | printf("a - b = %d\n", result); 220 | result = a * b; 221 | printf("a * b = %d\n", result); 222 | result = a / b; 223 | printf("a / b = %d\n", result); 224 | 225 | return 0; 226 | } 227 | ``` 228 | 229 | Since these are arithmetic operators, they will perform basic arithmetic operations. 230 | 231 | The result is: 232 | 233 | ``` 234 | where a = 10 & b = 5 235 | a + b = 15 236 | a - b = 5 237 | a * b = 50 238 | a / b = 2 239 | ``` 240 | 241 | ### Comparison Operators Sample Program 242 | ```c 243 | #include 244 | 245 | int main() { 246 | int a = 10, b = 10, c = 20; 247 | 248 | printf("%d == %d is %d\n", a, b, a == b); 249 | printf("%d == %d is %d\n", a, c, a == c); 250 | printf("%d > %d is %d\n", a, b, a > b); 251 | 252 | return 0; 253 | } 254 | ``` 255 | Since these are comparison operators, they compare the left and right side values. 256 | 257 | The result is: 258 | 259 | ``` 260 | 10 == 10 is 1 261 | 10 == 20 is 0 262 | 10 > 10 is 0 263 | ``` 264 | 265 | The result is either 0 or 1. Remember, 0 is FALSE and 1 is TRUE. 266 | 267 | ## Conditionals 268 | In a comprehensive program, the computer must make decisions based on given 269 | conditions. Of course, the computer cannot do this alone; you must instruct it 270 | exactly. The most common conditional statement is the `IF` statement, often 271 | extended with `IF/ELSE`. 272 | 273 | ### Sample Program Using IF 274 | ```c 275 | #include 276 | 277 | int main() { 278 | int i = 10; 279 | 280 | if (i == 10) { 281 | printf("Expected value is the same as variable i, so the result is TRUE.\n"); 282 | } 283 | 284 | return 0; 285 | } 286 | ``` 287 | 288 | In a single `IF` statement, the programmer wants to test, expect, or verify 289 | something. In this program, the programmer expects that the variable `i` has 290 | the value 10. Since variable `i` does have the value 10, the statement 291 | `Expected value is the same as variable i, so the result is TRUE.` will be 292 | printed. If the expected value is not the same as the value of the variable, 293 | the statement will not be printed. 294 | 295 | Sometimes, just an `IF` statement is not sufficient, particularly when you want 296 | to handle the FALSE result or create a nested `IF-ELSE`. In such cases, you 297 | extend it to catch the FALSE result. 298 | 299 | ### Sample Program Using IF ELSE 300 | ```c 301 | #include 302 | 303 | int main() { 304 | int i = 10; 305 | 306 | if (i == 11) { 307 | printf("Expected value is the same as variable i, so the result is TRUE.\n"); 308 | } else { 309 | printf("Expected value is not the same as variable i, so the result is FALSE.\n"); 310 | } 311 | 312 | return 0; 313 | } 314 | ``` 315 | 316 | Not only can the statement in the `ELSE` branch be printed, but you can also 317 | perform various actions such as correcting an error, navigating to a certain 318 | part of a program, etc. That's the power of catching the FALSE result. 319 | 320 | ## Loops 321 | There are commands or portions of your program to be repeated several times. 322 | Loops are there to do that. Now, there are simple loops and loops based on a 323 | given condition, much like a repeated IF statement. Simple loops are like 324 | "repeat 10 times" or "repeat forever." Conditional loops are loops with 325 | specific conditions other than simple iteration, just like in robot 326 | programming: "repeat until color red," "repeat until the distance is less than 327 | 50mm," etc. 328 | 329 | The most common loops that we see in computer programming are the `for` loop, 330 | `while` loop, and `do-while` loop. 331 | 332 | ### Sample Program Using Loops 333 | ```c 334 | #include 335 | 336 | int main() { 337 | int n = 11; 338 | int i; 339 | int x[n]; 340 | 341 | printf("The `for` loop:\n"); 342 | for (i = 1; i < n; i++) { 343 | printf("Iteration: %d | Hello World.\n", i); 344 | } 345 | 346 | int y = 0; 347 | printf("------\n"); 348 | printf("The `while` loop:\n"); 349 | while (y < 10) { 350 | y += 1; 351 | printf("Iteration: %d | Hello World.\n", y); 352 | } 353 | 354 | y = 0; 355 | printf("------\n"); 356 | printf("The `do-while` loop:\n"); 357 | do { 358 | y += 1; 359 | printf("Iteration: %d | Hello World.\n", y); 360 | } while (y < 10); 361 | 362 | return 0; 363 | } 364 | ``` 365 | 366 | The result: 367 | ``` 368 | The `for` loop: 369 | Iteration: 1 | Hello World. 370 | Iteration: 2 | Hello World. 371 | Iteration: 3 | Hello World. 372 | Iteration: 4 | Hello World. 373 | Iteration: 5 | Hello World. 374 | Iteration: 6 | Hello World. 375 | Iteration: 7 | Hello World. 376 | Iteration: 8 | Hello World. 377 | Iteration: 9 | Hello World. 378 | Iteration: 10 | Hello World. 379 | ------ 380 | The `while` loop: 381 | Iteration: 1 | Hello World. 382 | Iteration: 2 | Hello World. 383 | Iteration: 3 | Hello World. 384 | Iteration: 4 | Hello World. 385 | Iteration: 5 | Hello World. 386 | Iteration: 6 | Hello World. 387 | Iteration: 7 | Hello World. 388 | Iteration: 8 | Hello World. 389 | Iteration: 9 | Hello World. 390 | Iteration: 10 | Hello World. 391 | ------ 392 | The `do-while` loop: 393 | Iteration: 1 | Hello World. 394 | Iteration: 2 | Hello World. 395 | Iteration: 3 | Hello World. 396 | Iteration: 4 | Hello World. 397 | Iteration: 5 | Hello World. 398 | Iteration: 6 | Hello World. 399 | Iteration: 7 | Hello World. 400 | Iteration: 8 | Hello World. 401 | Iteration: 9 | Hello World. 402 | Iteration: 10 | Hello World. 403 | ``` 404 | 405 | As you can see here, it's just printing "Hello World" ten times, whether it's 406 | a `for` loop, `while` loop, or `do-while` loop. 407 | 408 | ## Functions 409 | A function is a group of statements (commands) that together perform a task. 410 | There are built-in functions and functions that a programmer will create 411 | according to his/her needs. 412 | 413 | ### Sample Program Creating And Using Functions 414 | ```c 415 | #include 416 | 417 | void myFunction() { 418 | printf("Hello World.\n"); 419 | printf("Life is beautiful.\n"); 420 | printf("Cherish every single moment.\n"); 421 | } 422 | 423 | int main() { 424 | myFunction(); 425 | return 0; 426 | } 427 | ``` 428 | 429 | The function `myFunction()` is created by declaring `void` first. Other 430 | functions will return values, but for simplicity, we create a void function. 431 | A `void` function will simply execute the commands contained in it, nothing 432 | else. 433 | 434 | Then inside the main function, you simply invoke it by its name. All the 435 | commands inside that function will be executed line by line. 436 | 437 | Why use functions? Imagine if there were none. Complex programs are usually 438 | composed of thousands to millions of lines of code. Without any grouping, 439 | that would be very hard to handle. 440 | 441 | If a function is generic and can be used for other projects, you can simply 442 | separate it for distribution. So your code is now reusable. 443 | 444 | ## More Of My Content 445 | - [jdevfullstack Profile](https://github.com/jdevfullstack) 446 | - [jdevfullstack Repos](https://github.com/jdevfullstack?tab=repositories) 447 | - [jdevfullstack Projects](https://github.com/jdevfullstack-projects) 448 | - [jdevfullstack Tutorials](https://github.com/jdevfullstack-tutorials) 449 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman --------------------------------------------------------------------------------