How Coding Works
Beginner Series: What Is Coding?
So how does coding work, really? The short answer is that writing code tells the computer what to do, but it’s not quite that simple.
So here’s the longer answer. A computer can only understand two distinct types of data: on and off. In fact, a computer is really just a collection of on/off switches (transistors). Anything that a computer can do is nothing more than a unique combination of some transistors turned on and some transistors turned off.
Binary code is the representation of these combinations as 1s and 0s, where each digit represents one transistor. Binary code is grouped into bytes, groups of 8 digits representing 8 transistors. For example, 11101001. Modern computers contain millions or even billions of transistors, which means an unimaginably large number of combinations.
But one problem arises here. To be able to write a computer program by typing out billions of 1s and 0s would require superhuman brainpower, and even then it would probably take you a lifetime or two to write.
This is where programming languages come in…
Programming Languages
Here’s a simple example of some code:
print 'Hello, world!'
That line of code is written in the Python programming language. Put simply, a programming (or coding) language is a set of syntax rules that define how code should be written and formatted.
Thousands of different programming languages make it possible for us to create computer software, apps and websites. Instead of writing binary code, they let us write code that is (relatively) easy for us to write, read and understand. Each language comes with a special program that takes care of translating what we write into binary code.
Why Do We Have So Many Languages?
Because different languages are designed to be used for different purposes – some are useful for web development, others useful for writing desktop software, others useful for solving scientific and numeric problems, and so on.
Low-Level and High-Level Languages
Programming languages can also be low-level or high-level.
Low-level languages are closer to the binary code a computer understands, while high-level languages bear a lot less resemblance to binary code. High-level languages are easier to program in, because they’re less detailed and designed to be easy for us to write.
Nearly all of the main programming languages in use today are high-level languages.
Programs
A program is simply a text file, written in a certain coding language. The code inside a program file is called the source code. Every coding language has its own file extension for identifying code files written in that language. For example, Python’s is ‘.py’.
To make a program, you write the code in a plain text editor like Notepad and save the file to your computer. That’s it. For example, the below line of code could be the contents of a very short Python program called hello.py:
print 'Hello, world!'
How do you run a program and actually get it to perform its commands? That varies between coding languages. Some languages save a separate binary file that the computer can directly run, while other languages have their programs run indirectly by certain software.
For example, a JavaScript program file would get run by a web browser like Chrome. A PHP program file would get run by a web server like LAMP.
In the case of our hello.py file, the Python language comes with a command line which will display the output of the program – the text ‘Hello, world!’. If you were to enter the code into the command line and press enter, the program gets run and the command will get executed.
What Happens When You Run a Program
A computer doesn’t actually understand the phrase ‘Hello, world!’, and it doesn’t know how to display it on screen. It only understands on and off. So to actually run a command like
print 'Hello, world!'
, it has to translate all the code in a program into a series of ons and offs that it can understand.
To do that, a number of things happen:
- The source code is translated into assembly language.
- The assembly code is translated into machine language.
- The machine language is directly executed as binary code.
Confused? Let’s go into a bit more detail. The coding language first has to translate its source code into assembly language, a super low-level language that uses words and numbers to represent binary patterns. Depending on the language, this may be done with an interpreter (where the program is translated line-by-line), or with a compiler (where the program is translated as a whole).
The coding language then sends off the assembly code to the computer’s assembler, which converts it into the machine language that the computer can understand and execute directly as binary code.
Conclusion
Isn’t it amazing to think that something as deceptively simple and primitive as binary code can create things as complex as what goes on inside a computer?
Your screen, operating system, photos, videos, the Internet, Facebook, your online bank account, and this website – all these things can be constructed from nothing but 1s and 0s. It’s a real symbol of human achievement.
Don’t worry if this process seems complicated and confusing – the whole reason that coding languages exist is to simplify it all for you! On the next page, you’ll learn about 12 of today’s popular languages.
No comments:
Post a Comment