diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..9e50aa4 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: jdevstatic diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 49681c9..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020 xdvrx1 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index 21635ab..3f981dd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # Core Concepts of Computer Programming -Table of Contents +*`updated July 31, 2024`* + +[![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) + +TOC 1. [Intro](#intro) 2. [Input / Output](#input-and-output) 1. [Hello World Program](#hello-world-program) @@ -13,59 +17,54 @@ Table of Contents 5. [Conditionals](#conditionals) 1. [Sample Program Using IF](#sample-program-using-if) 2. [Sample Program Using IF/ELSE](#sample-program-using-if-else) -6. [Functions](#functions) +6. [Loops](#loops) + 1. [Sample Program Using Loops](#sample-program-using-loops) +7. [Functions](#functions) 1. [Sample Program Creating And Using Functions](#sample-program-creating-and-using-functions) +8. [More Of My Content](#more-of-my-content) ## Intro -Of all the inventions in the field of electronics -like the radio and telephone, the programmable computer -was the most significant one. It changed the -world forever. - -If ever a certain device -can be programmed the way we want it to behave, then -we can simply instruct it to do things for us. -That's very practical, isn't it? - -After the programmable computers, the next -big question was how humans would program these computers. -They began developing programming languages and -the 1970s was a very remarkable period: -the C language was invented. - -After the success, the great potential was unleashed -and we are now seeing the things a programmable computer -can do: the era of information, real-time data, -almost realistic 3D games, artificial intelligence, etc. - -Learning computer science now is a very sophisticated -one unlike it was in the past: there are too many -fields even in software development alone. - -And if you are a beginner, you might be discouraged -from all the things you need to learn but don't worry: -**you don't need to learn them all**. What you need -to learn first is the understanding how computers work -and the fundamentals of computer programming that -all computer programming languages have in common. - -And since almost every programming language nowadays -directly and indirectly inherits concepts from -the C language, we'll be using it to demonstrate -the core concepts of programming. - -*Take note: we don't want to discuss every -detail of the code in terms of the C language, -rather we just want to get the core -concepts of computer programming that are -common in different programming languages.* +Of all the inventions in the field of electronics like the radio and +telephone, the programmable computer was the most significant one. It +changed the world forever. + +If a certain device can be programmed the way we want it to behave, +then we can simply instruct it to do things for us. That's very +practical, isn't it? + +After programmable computers, the next big question was how humans +would program them. They began developing programming languages and +the 1970s was a very remarkable period: the C language was invented. + +After this success, great potential was unleashed, and we are now +seeing what a programmable computer can do: the era of information, +real-time data, almost realistic 3D games, artificial intelligence, +etc. + +Learning computer science today, unlike in the past, involves many +specialized fields, even in software development alone. + +And if you are a beginner, you might be discouraged from all the things +you need to learn but don't worry: **you don't need to learn them all**. +What you need to learn first is understanding how computers work and +the fundamentals of computer programming that all computer programming +languages have in common. + +And since almost every programming language nowadays directly and +indirectly inherits concepts from the C language, we'll be using it to +demonstrate the core concepts of programming. + +*Take note: we don't want to discuss every detail of the code in terms +of the C language, rather we just want to get the core concepts of +computer programming that are common in different programming +languages.* ## Input And Output As was mentioned, a programmable computer is very -important but the way you interact with the computer -is through input and output: you as the user -will have an input, the computer will process it -(the way it is processed is programmed also), +important, but the way you interact with the computer +is through input and output: you, as the user, +will provide input, the computer will process it +(the way it is processed is also programmed), then the computer will output something. The very first demonstration is the "Hello World" program. The programmer will tell the computer @@ -76,17 +75,16 @@ as that. Typical programs like binary to human language (and vice versa) takes a lot of processing, an operating system to recognize computer applications, -a business collaborative tool with a lot of features +a business collaborative tool with many features to manage data, a 3D game that is almost realistic, an Artificial Intelligence program that can simulate humans in the virtual world! But of all the things programmers can do, the computer only processes input as binary data -(0s and 1s) and -it should not be interpreted literally. It's just -a representation of something a computer -can recognize: absence or presence of electric pulse. +(0s and 1s), and it should not be interpreted literally. +It's just a representation of something a computer +can recognize: the absence or presence of an electric pulse. Compare this with any drawing that can be accomplished just by simply using a dot-and-no-dot pattern! @@ -94,7 +92,7 @@ It's the same thing for the computer. ### Hello World Program -``` +```c #include int main() { @@ -107,53 +105,41 @@ int main() { } ``` -In this small program, the `#include` -tells the computer to include a certain -source which is needed because -it contains the predefined command `printf`. -`printf` simply tells the computer -to display the text provided by the programmer. +In this small program, the `#include` tells the computer to include a +certain source which is needed because it contains the predefined +command `printf`. `printf` simply tells the computer to display the +text provided by the programmer. -As simple as that, you have now the complete -idea of what a programmable computer is all about. -It's you telling the computer to do things it -can handle in terms of binary data. +As simple as that, you now have the complete idea of what a +programmable computer is all about. It's you telling the computer to +do things it can handle in terms of binary data. ## Variables And Data Types -In computer programming, a **variable** is just like -a container to store digital data. A **data type** -is the way you tell the computer how that data -will be interpreted. Should the computer interpret -that as letters? or numbers? or words? or simply -raw binary data? The computer does not know -that, you must tell it exactly. - -In computer programming, both these things -will enable you to store data and tell -the computer what kind of data it is. When you -store, you want to retrieve it later as per -needed. Remember also, computer's memory -is different from storage. Think of it -as the short-term memory and the storage -(the hard disk) as the long-term memory. -But during the runtime of a program you -are developing, you refer to memory as -`storage`. The computer's memory has the full -access to the CPU while the disk does not have -that access. -In order to read contents from the hard disk, -a request should be made. Hence, in programming -you are using the memory first not the disk. -There is the current development to combine -the two but is still on-going. - -Take note, the details of memory, hard disk -and CPU are quite complex but the mentioned -details above will serve as the starting point -for full comprehension. + +In computer programming, a **variable** is just like a container to +store digital data. A **data type** is the way you tell the computer +how that data will be interpreted. Should the computer interpret that +as letters? or numbers? or words? or simply raw binary data? The +computer does not know that, you must tell it exactly. + +In computer programming, both these things will enable you to store +data and tell the computer what kind of data it is. When you store, +you want to retrieve it later as needed. Remember also, computer's +memory is different from storage. Think of it as the short-term memory +and the storage (the hard disk) as the long-term memory. But during +the runtime of a program you are developing, you refer to memory as +`storage`. The computer's memory has full access to the CPU while the +disk does not have that access. In order to read contents from the +hard disk, a request should be made. Hence, in programming, you are +using the memory first, not the disk. There is current development to +combine the two, but it is still ongoing. + +Take note, the details of memory, hard disk, and CPU are quite complex, +but the mentioned details above will serve as the starting point for +full comprehension. ### Sample Program for Variables and Data Types -``` +```c #include int main() { @@ -170,24 +156,21 @@ int main() { } ``` -In this simple program, we declare and assign 3 -variables with different data types: -1. variable `i` that contains 1 numeric value -2. variable character `myletter[]` -that contains letters `myletter` -3. variable `x` that contains 1.23 value - -After that, the program is telling the computer -to display these values. +In this simple program, we declare and assign +three variables with different data types: +1. an integer variable `i` that contains the numeric value `1` +2. a character array `myletter[]` that contains the string `"myletter"` +3. a float variable `x` that contains the value `1.23` +After that, the program instructs the computer to display these values. ## Operators -In programming just like in mathematics, there are -operators and the most common in computer programming -are assignment, arithmetic and comparison operators. +In programming, just like in mathematics, there are operators. The most +common in computer programming are assignment, arithmetic, and +comparison operators. ### Assignment Operators Sample Program -``` +```c #include int main() { @@ -204,12 +187,11 @@ int main() { } ``` -The most common assignment operator is -`=` and it should not be confused with -the double `==` comparison operator. +The most common assignment operator is `=`. It should not be confused +with the double `==` comparison operator. -This time, it is simply assigning a -value to a variable. The result is: +In this example, the `=` operator is simply assigning a value to a +variable. The result is: ``` c = 10 @@ -217,11 +199,11 @@ c = 15 c = 3 ``` -Variable `c` contains a value at a specific time. -It was changed in the program three times. +Variable `c` contains a value at a specific time. It was changed in the +program three times. ### Arithmetic Operators Sample Program -``` +```c #include int main() { @@ -229,225 +211,207 @@ int main() { int b = 5; int result; - printf("where a = %d & b = %d \n", a, b); + printf("where a = %d & b = %d\n", a, b); result = a + b; - printf("a+b = %d \n", result); + printf("a + b = %d\n", result); result = a - b; - printf("a-b = %d \n", result); + printf("a - b = %d\n", result); result = a * b; - printf("a*b = %d \n", result); + printf("a * b = %d\n", result); result = a / b; - printf("a/b = %d \n", result); + printf("a / b = %d\n", result); return 0; } ``` -Since these are arithmetic operators, they -will do the basic arithmetic operations. + +Since these are arithmetic operators, they will perform basic arithmetic operations. The result is: ``` -where a = 10 & b = 5 -a+b = 15 -a-b = 5 -a*b = 50 -a/b = 2 +where a = 10 & b = 5 +a + b = 15 +a - b = 5 +a * b = 50 +a / b = 2 ``` ### Comparison Operators Sample Program -``` +```c #include int main() { int a = 10, b = 10, c = 20; - printf("%d == %d is %d \n", a, b, a == b); - printf("%d == %d is %d \n", a, c, a == c); - printf("%d > %d is %d \n", a, b, a > b); + printf("%d == %d is %d\n", a, b, a == b); + printf("%d == %d is %d\n", a, c, a == c); + printf("%d > %d is %d\n", a, b, a > b); return 0; } ``` -Since these are comparison operators, they are -comparing the left and right side values. +Since these are comparison operators, they compare the left and right side values. The result is: ``` -10 == 10 is 1 -10 == 20 is 0 -10 > 10 is 0 +10 == 10 is 1 +10 == 20 is 0 +10 > 10 is 0 ``` -The result is either 0 or 1 and remember, -0 is FALSE and 1 is TRUE. +The result is either 0 or 1. Remember, 0 is FALSE and 1 is TRUE. ## Conditionals -In a comprehensive program, the computer must decide -based on the condition/s given. Of course, the computer -cannot do that alone, you must instruct it exactly. -The most common is the -IF statement with the extended IF/ELSE. +In a comprehensive program, the computer must make decisions based on given +conditions. Of course, the computer cannot do this alone; you must instruct it +exactly. The most common conditional statement is the `IF` statement, often +extended with `IF/ELSE`. ### Sample Program Using IF -``` +```c #include int main() { int i = 10; - if (i == 10) printf("Expected value is the same as variable i, so the result is TRUE. \n"); + if (i == 10) { + printf("Expected value is the same as variable i, so the result is TRUE.\n"); + } return 0; } ``` -In a single IF statement, the programmer wants to test, -expect or verify something, such as this program. -The programmer is expecting that `i` variable -has the value 10, and variable `i` has the same -value, so the statement -`Expected value is the same as variable i, so the result is TRUE.` -will be printed. If the expected value is -not the same as the value of the variable, +In a single `IF` statement, the programmer wants to test, expect, or verify +something. In this program, the programmer expects that the variable `i` has +the value 10. Since variable `i` does have the value 10, the statement +`Expected value is the same as variable i, so the result is TRUE.` will be +printed. If the expected value is not the same as the value of the variable, the statement will not be printed. -Sometimes, just an IF will not be sufficient, -particularly when you want to catch the FALSE result -or create a nested IF-ELSE. So, you want to extend it -and catch the FALSE result. +Sometimes, just an `IF` statement is not sufficient, particularly when you want +to handle the FALSE result or create a nested `IF-ELSE`. In such cases, you +extend it to catch the FALSE result. ### Sample Program Using IF ELSE -``` +```c #include int main() { int i = 10; if (i == 11) { - printf("Expected value is the same as variable i, so the result is TRUE. \n"); + printf("Expected value is the same as variable i, so the result is TRUE.\n"); } else { - printf("Expected value is not the same as variable i, so the result is FALSE. \n"); + printf("Expected value is not the same as variable i, so the result is FALSE.\n"); } + return 0; } ``` -Not only the statement in the ELSE branch will be printed, -you can do a lot of things just like correct an error, -go to a certain part of a program, etc. That's the power -of catching the FALSE result. +Not only can the statement in the `ELSE` branch be printed, but you can also +perform various actions such as correcting an error, navigating to a certain +part of a program, etc. That's the power of catching the FALSE result. ## Loops -There are commands or portions of your program -to be repeated several times. Loops are there to do -that. Now, there are simple loops and -loops based on a given condition, -much like a repeated IF statement. -Simple loops are like repeat 10 times or repeat -forever. Conditional loops are loops with -specific conditions other than simple iteration -just like in robot programming: -repeat until color red, repeat until the -distance is less than 50mm, etc. - -The most common that we see in computer -programming: the `for` loop, -`while` loop and `do-while` loop. +There are commands or portions of your program to be repeated several times. +Loops are there to do that. Now, there are simple loops and loops based on a +given condition, much like a repeated IF statement. Simple loops are like +"repeat 10 times" or "repeat forever." Conditional loops are loops with +specific conditions other than simple iteration, just like in robot +programming: "repeat until color red," "repeat until the distance is less than +50mm," etc. + +The most common loops that we see in computer programming are the `for` loop, +`while` loop, and `do-while` loop. ### Sample Program Using Loops -``` +```c #include int main() { - int n = 11; - int i; int x[n]; - printf("the `for` loop: \n"); - for (i = 1; i < n; i++) { - printf("iteration: %d | Hello World. \n", i); + printf("The `for` loop:\n"); + for (i = 1; i < n; i++) { + printf("Iteration: %d | Hello World.\n", i); } int y = 0; - printf("------ \n"); - printf("the `while` loop: \n"); + printf("------\n"); + printf("The `while` loop:\n"); while (y < 10) { y += 1; - printf("iteration: %d | Hello World. \n", y); + printf("Iteration: %d | Hello World.\n", y); } y = 0; - - printf("------ \n"); - printf("the `do while` loop: \n"); - + printf("------\n"); + printf("The `do-while` loop:\n"); do { y += 1; - printf("iteration: %d | Hello World. \n", y); - } - while (y < 10); + printf("Iteration: %d | Hello World.\n", y); + } while (y < 10); return 0; - } ``` -the result: +The result: ``` -the `for` loop: -iteration: 1 | Hello World. -iteration: 2 | Hello World. -iteration: 3 | Hello World. -iteration: 4 | Hello World. -iteration: 5 | Hello World. -iteration: 6 | Hello World. -iteration: 7 | Hello World. -iteration: 8 | Hello World. -iteration: 9 | Hello World. -iteration: 10 | Hello World. ------- -the `while` loop: -iteration: 1 | Hello World. -iteration: 2 | Hello World. -iteration: 3 | Hello World. -iteration: 4 | Hello World. -iteration: 5 | Hello World. -iteration: 6 | Hello World. -iteration: 7 | Hello World. -iteration: 8 | Hello World. -iteration: 9 | Hello World. -iteration: 10 | Hello World. ------- -the `do while` loop: -iteration: 1 | Hello World. -iteration: 2 | Hello World. -iteration: 3 | Hello World. -iteration: 4 | Hello World. -iteration: 5 | Hello World. -iteration: 6 | Hello World. -iteration: 7 | Hello World. -iteration: 8 | Hello World. -iteration: 9 | Hello World. -iteration: 10 | Hello World. +The `for` loop: +Iteration: 1 | Hello World. +Iteration: 2 | Hello World. +Iteration: 3 | Hello World. +Iteration: 4 | Hello World. +Iteration: 5 | Hello World. +Iteration: 6 | Hello World. +Iteration: 7 | Hello World. +Iteration: 8 | Hello World. +Iteration: 9 | Hello World. +Iteration: 10 | Hello World. +------ +The `while` loop: +Iteration: 1 | Hello World. +Iteration: 2 | Hello World. +Iteration: 3 | Hello World. +Iteration: 4 | Hello World. +Iteration: 5 | Hello World. +Iteration: 6 | Hello World. +Iteration: 7 | Hello World. +Iteration: 8 | Hello World. +Iteration: 9 | Hello World. +Iteration: 10 | Hello World. +------ +The `do-while` loop: +Iteration: 1 | Hello World. +Iteration: 2 | Hello World. +Iteration: 3 | Hello World. +Iteration: 4 | Hello World. +Iteration: 5 | Hello World. +Iteration: 6 | Hello World. +Iteration: 7 | Hello World. +Iteration: 8 | Hello World. +Iteration: 9 | Hello World. +Iteration: 10 | Hello World. ``` -As you can see here, it's just printing the -Hello World ten times, whether it's `for` loop, -`while` loop or `do-while` loop. +As you can see here, it's just printing "Hello World" ten times, whether it's +a `for` loop, `while` loop, or `do-while` loop. ## Functions -A function is a group of statements (commands) -that together perform a task. -There are built-in functions and functions that a -programmer will create according to his/her needs. +A function is a group of statements (commands) that together perform a task. +There are built-in functions and functions that a programmer will create +according to his/her needs. ### Sample Program Creating And Using Functions -``` +```c #include void myFunction() { @@ -457,28 +421,28 @@ void myFunction() { } int main() { - myFunction(); - return 0; } ``` -The function `printFunctionTest()` is created by -declaring `void` first. Other functions will return values -but for the sake of simplicity we create a void `function`. -A `void` function will simply execute the commands -contained in it, nothing else. +The function `myFunction()` is created by declaring `void` first. Other +functions will return values, but for simplicity, we create a void function. +A `void` function will simply execute the commands contained in it, nothing +else. + +Then inside the main function, you simply invoke it by its name. All the +commands inside that function will be executed line by line. -Then inside the main function, you simply invoke it -by its name. And all the commands inside that function -will be executed line by line. +Why use functions? Imagine if there were none. Complex programs are usually +composed of thousands to millions of lines of code. Without any grouping, +that would be very hard to handle. -Why use functions? Imagine if there is none. Complex -programs are usually composed of thousands of lines of code -up to million, without any grouping, that will be -very hard to handle. +If a function is generic and can be used for other projects, you can simply +separate it for distribution. So your code is now reusable. -If a function is generic and can be used for other -projects, you can simply separate it for distribution. -So your code is now reusable. +## More Of My Content +- [jdevfullstack Profile](https://github.com/jdevfullstack) +- [jdevfullstack Repos](https://github.com/jdevfullstack?tab=repositories) +- [jdevfullstack Projects](https://github.com/jdevfullstack-projects) +- [jdevfullstack Tutorials](https://github.com/jdevfullstack-tutorials)