Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5
jfullstackdev/programming-core-concepts
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
updated July 31, 2024
TOC
Of all the inventions in the field of electronics like the radio andtelephone, the programmable computer was the most significant one. Itchanged 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 verypractical, isn't it?
After programmable computers, the next big question was how humanswould program them. They began developing programming languages andthe 1970s was a very remarkable period: the C language was invented.
After this success, great potential was unleashed, and we are nowseeing 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 manyspecialized fields, even in software development alone.
And if you are a beginner, you might be discouraged from all the thingsyou 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 andthe fundamentals of computer programming that all computer programminglanguages have in common.
And since almost every programming language nowadays directly andindirectly inherits concepts from the C language, we'll be using it todemonstrate the core concepts of programming.
Take note: we don't want to discuss every detail of the code in termsof the C language, rather we just want to get the core concepts ofcomputer programming that are common in different programminglanguages.
As was mentioned, a programmable computer is veryimportant, but the way you interact with the computeris 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 computerto simply display the message on the screen.
But of course, complex programs are not as straightforwardas that. Typical programs like binaryto human language (and vice versa)takes a lot of processing,an operating system to recognize computer applications,a business collaborative tool with many featuresto manage data, a 3D game that is almost realistic,an Artificial Intelligence program that can simulate humansin the virtual world!
But of all the things programmers can do, thecomputer only processes input as binary data(0s and 1s), and it should not be interpreted literally.It's just a representation of something a computercan recognize: the absence or presence of an electric pulse.Compare this with any drawingthat can be accomplished justby simply using a dot-and-no-dot pattern!It's the same thing for the computer.
#include<stdio.h>intmain() {printf("Hello World!\n");printf("Life is a blessing!\n");return0;}
In this small program, the#include tells the computer to include acertain source which is needed because it contains the predefinedcommandprintf.printf simply tells the computer to display thetext provided by the programmer.
As simple as that, you now have the complete idea of what aprogrammable computer is all about. It's you telling the computer todo things it can handle in terms of binary data.
In computer programming, avariable is just like a container tostore digital data. Adata type is the way you tell the computerhow that data will be interpreted. Should the computer interpret thatas letters? or numbers? or words? or simply raw binary data? Thecomputer does not know that, you must tell it exactly.
In computer programming, both these things will enable you to storedata and tell the computer what kind of data it is. When you store,you want to retrieve it later as needed. Remember also, computer'smemory is different from storage. Think of it as the short-term memoryand the storage (the hard disk) as the long-term memory. But duringthe runtime of a program you are developing, you refer to memory asstorage. The computer's memory has full access to the CPU while thedisk does not have that access. In order to read contents from thehard disk, a request should be made. Hence, in programming, you areusing the memory first, not the disk. There is current development tocombine 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 forfull comprehension.
#include<stdio.h>intmain() {inti=1;charmyletter[]="myletter";floatx=1.23;printf("int i: %d\n",i);printf("char myletter[]: %s\n",myletter);printf("float x: %f\n",x);return0;}
In this simple program, we declare and assignthree variables with different data types:
- an integer variable
ithat contains the numeric value1 - a character array
myletter[]that contains the string"myletter" - a float variable
xthat contains the value1.23
After that, the program instructs the computer to display these values.
In programming, just like in mathematics, there are operators. The mostcommon in computer programming are assignment, arithmetic, andcomparison operators.
#include<stdio.h>intmain() {intc;c=10;printf("c = %d\n",c);c=15;printf("c = %d\n",c);c=3;printf("c = %d\n",c);return0;}
The most common assignment operator is=. It should not be confusedwith the double== comparison operator.
In this example, the= operator is simply assigning a value to avariable. The result is:
c = 10c = 15c = 3Variablec contains a value at a specific time. It was changed in theprogram three times.
#include<stdio.h>intmain() {inta=10;intb=5;intresult;printf("where a = %d & b = %d\n",a,b);result=a+b;printf("a + b = %d\n",result);result=a-b;printf("a - b = %d\n",result);result=a*b;printf("a * b = %d\n",result);result=a /b;printf("a / b = %d\n",result);return0;}
Since these are arithmetic operators, they will perform basic arithmetic operations.
The result is:
where a = 10 & b = 5a + b = 15a - b = 5a * b = 50a / b = 2#include<stdio.h>intmain() {inta=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);return0;}
Since these are comparison operators, they compare the left and right side values.
The result is:
10 == 10 is 110 == 20 is 010 > 10 is 0The result is either 0 or 1. Remember, 0 is FALSE and 1 is TRUE.
In a comprehensive program, the computer must make decisions based on givenconditions. Of course, the computer cannot do this alone; you must instruct itexactly. The most common conditional statement is theIF statement, oftenextended withIF/ELSE.
#include<stdio.h>intmain() {inti=10;if (i==10) {printf("Expected value is the same as variable i, so the result is TRUE.\n"); }return0;}
In a singleIF statement, the programmer wants to test, expect, or verifysomething. In this program, the programmer expects that the variablei hasthe value 10. Since variablei does have the value 10, the statementExpected value is the same as variable i, so the result is TRUE. will beprinted. If the expected value is not the same as the value of the variable,the statement will not be printed.
Sometimes, just anIF statement is not sufficient, particularly when you wantto handle the FALSE result or create a nestedIF-ELSE. In such cases, youextend it to catch the FALSE result.
#include<stdio.h>intmain() {inti=10;if (i==11) {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"); }return0;}
Not only can the statement in theELSE branch be printed, but you can alsoperform various actions such as correcting an error, navigating to a certainpart of a program, etc. That's the power of catching the FALSE result.
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 agiven condition, much like a repeated IF statement. Simple loops are like"repeat 10 times" or "repeat forever." Conditional loops are loops withspecific conditions other than simple iteration, just like in robotprogramming: "repeat until color red," "repeat until the distance is less than50mm," etc.
The most common loops that we see in computer programming are thefor loop,while loop, anddo-while loop.
#include<stdio.h>intmain() {intn=11;inti;intx[n];printf("The `for` loop:\n");for (i=1;i<n;i++) {printf("Iteration: %d | Hello World.\n",i); }inty=0;printf("------\n");printf("The `while` loop:\n");while (y<10) {y+=1;printf("Iteration: %d | Hello World.\n",y); }y=0;printf("------\n");printf("The `do-while` loop:\n");do {y+=1;printf("Iteration: %d | Hello World.\n",y); }while (y<10);return0;}
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.As you can see here, it's just printing "Hello World" ten times, whether it'safor loop,while loop, ordo-while loop.
A function is a group of statements (commands) that together perform a task.There are built-in functions and functions that a programmer will createaccording to his/her needs.
#include<stdio.h>voidmyFunction() {printf("Hello World.\n");printf("Life is beautiful.\n");printf("Cherish every single moment.\n");}intmain() {myFunction();return0;}
The functionmyFunction() is created by declaringvoid first. Otherfunctions will return values, but for simplicity, we create a void function.Avoid function will simply execute the commands contained in it, nothingelse.
Then inside the main function, you simply invoke it by its name. All thecommands inside that function will be executed line by line.
Why use functions? Imagine if there were none. Complex programs are usuallycomposed of thousands to millions of lines of code. Without any grouping,that would be very hard to handle.
If a function is generic and can be used for other projects, you can simplyseparate it for distribution. So your code is now reusable.
About
no idea what computer programming is all about? read this
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.