Computer Programming: Every PC user should know how to program, and there's never been a better time to learn
With the huge variety of computing devices all around us, it's important to remember what it is that's special about a full-fledged personal computer. We think the main difference can be summed up in one word: mastery. No matter how much time you spend with an iPad or an Android phone or in a web browser, you can never truly master it. There's just not enough there to learn. But the PC? That's different. The PC goes deep.
As you develop your mastery over the PC, you move past all sorts of boundaries. First, you learn to replace the software that came on the computer. You discover the command prompt and how to tweak the OS. You learn to build your own PC, and to benchmark it. And then, at the very bottom of it all, there's one last boundary standing between you and true PC mastery. You have to learn computer programming.
Why is coding the ultimate test of PC mastery? Because learning how to program is the thing that breaks down the wall between you and your computer—it makes it possible for you to truly understand what's going on underneath your desktop.
And, all philosophical ramblings aside, it's a pretty great skill to have. Whether you need to automate a process on your computer or whip up a quick web app for a family member's website, knowing how to code is a big boon. Who knows, you might even be able to earn some money.
Learning to program isn't something you can do in an hour, or even in an afternoon. You have to learn to think in a whole new way, which takes dedication and patience. Fortunately, it can also be a lot of fun. In this article, we're going to take a whirlwind tour through some of the most important concepts in computer programming, and we'll direct you to resources that'll help you start your adventures in coding.
Basic Information
A Q&A on the ABCs of programming
Before we can do anything, we've got to cover the basics. Here's what you have to know before you can get started.
When we say computer "programming," what does that really mean?
For this article, we're going to use a fairly narrow definition of programming, and say that what we're talking about is the process of creating software on a computer. That process involves writing out a series of commands for the computer to execute, which will create our desired behavior. We write those commands using a programming language.
What's a programming language?
A programming language is the set of rules that define how we express what the computer should do when it executes the program. There's an incredible variety of programming languages available for use, but the vast majority of commercial and personal software is written in one of a core group of languages including C/C++, Java, C#, Python, and a few others. Modern programming languages share a lot of the same basic concepts and some syntax, so learning your second, third, or fourth programming language is much easier than learning your first.
What makes one programming language different from another?
Each programming language has its own strengths and weaknesses. C and C++ are low-level languages, meaning that code written in C is closer to the machine code that your CPU understands (see below). Low-level languages can produce faster, more efficient software, so they're used where performance is at a premium—for programming an operating system or a 3D gaming engine, for instance. High-level languages, like Java and Python, have the advantage of being much easier to program in, and the same program can generally be written with fewer lines of code in a high-level language.
But which one's the best?
There's no one best language—it really depends on what kind of programming you want to do. If you want to program native Windows applications, you'll use C#; if you want to program sophisticated web applications, Ruby would be a good choice; if you want to be the next John Carmack, you should probably start with C.
No, for real, which language should I start with?
The secret is to not stress too much about whichever particular language you start with. The important things you will be learning are all basic concepts that work pretty much the same in every programming language. You'll learn how to use data structures and conditionals and loops to manage how your code flows. You'll learn to structure your program in a way that's readable and organized. Once you've done all that, learning a bit of syntax to pick up a new language won't seem like much work at all.
But, if you really want a suggestion, start with JavaScript. It's an easy language to learn, it's got some practical applications, and its syntax is similar enough to some more-powerful languages like C# and Java that making the transition later on won't be too hard.
Is HTML a programming language?
Not quite! HTML is a markup language, used to define the contents of a webpage. Although HTML has a specific syntax (a set of rules defining how you have to write things), it doesn't have semantics, or meaning. An HTML document is rendered, rather than executed. That said, if you have written an HTML document, you at least have experience writing a formalized computer language, which may make the jump to programming easier.
What's an IDE?
An IDE (short for integrated development environment) is the software suite programmers use to actually write programs. They generally include a specialized text editor for writing the source code, as well as the ability to test and debug your program. Two of the most popular IDEs are Eclipse (open source, free, and available at www.eclipse.org) and Microsoft Visual Studio (proprietary and expensive, but with a free "Express" version that's limited to and excels at programming in C, C#, and BASIC).
Visual Studio is one of the most advanced IDEs around, and is used by nearly all Windows programmers.
How can I start writing a program, Like, right now?
Unfortunately, it can be a bit of a hassle to get started coding in most programming languages. You generally have to install and configure an SDK (software developer kit), and sometimes an IDE as well, in order to be able to write and compile code in a new language. It's rarely super hard, but be prepared to spend 15–30 minutes Googling, reading a guide for your chosen language, and setting things up.
Fortunately, JavaScript is much easier to get started with. In fact, you can start writing code right this second, using an in-browser coding environment like JSFiddle.net. An in-browser IDE isn't a good solution for serious programming projects, but it's a great way to get started as a beginner. To start writing JavaScript in an interactive environment with structured lessons, you can visit www.codeacademy.com (but more on that later).
Click the next page to learn about how it all works.
How Does It Actually Work?
When you write a program in a high-level language like Java-Script, the document you create isn't something that your computer's low-level hardware can understand. The CPU has only a limited number of instructions it can perform, such as addition, subtraction, and moving numbers into and out of memory. These instructions are actually physically implemented in the CPU using transistors organized into logic gates. Though modern instruction sets, such as the X86-64 set implemented in any consumer 64-bit CPU, are actually very large and sophisticated, programming for the CPU directly (using a super-low-level language called assembly language) is an arduous, slow process.
High-level languages allow you forgo a lot of the technical grunt work. For instance, in a high-level language, you can simply declare and use variables as you please, without ever worrying about what exactly is going on in your system's memory. In assembly language, you have to manually assign data to locations in memory as you use it, and clear up the memory when you're done.
In order to get your high-level program to run on the CPU, you need a compiler—a piece of software that optimizes your code and converts it into a machine-readable executable file. Some languages, such as Java, are not compiled, but rather interpreted, which means that the source code itself is distributed, and then compiled on the end user's machine. The upside of an interpreted language is that you can distribute a single file that can be run on Windows, OSX, or Linux. The downside is that whoever runs the file has to have a copy of the interpreter on their machine—an annoyance that anyone who's tried to run a Java-Script applet on a new computer will be familiar with.
Core Concepts
Understand these, and you've got everything you need to start writing programs in any language
Variables
Variables in programming are a little different from the "X"s you remember from high school algebra. In programming, a variable is like an empty container—it can hold a number, a word, or any other data or data structure you want to use in your program. The program can read and change the variable's value as it runs, letting you keep track of and manipulate data.
Variables are the basic building block of a program, and most lines of code in any program will include a variable in some form.
In some languages, such as Python, a single variable can contain one type of data (say, a number), then can be assigned to hold a different type of data (like a word). In other languages, such as C and C#, a variable is declared, with a particular type, and then can only hold that type of data for the rest of the program. This is the distinction between dynamically typed and statically typed programming languages.
Conditionals
Most programs do not run in a vacuum—they accept some form of user input. To deal with the uncertainty that this brings, we need to be able to write code that is flexible, and to do that, we need conditionals.
Conditionals are places where the code branches. In most modern languages, they take the form of an if statement, which joins an expression that is either true or false (called a Boolean expression) and a block of code. The if statement says, in a nutshell, "If this Boolean expression is true, execute the following code. Otherwise, skip it."
In most languages, if statements can also include an else clause, which allows you to specify a second block of code that will only be executed when the Boolean expression is false. For example, under the "Sample Code" section below, the 99BottlesOfBeer function includes an if statement that checks to see if the "age" variable is greater than or equal to 21, and sets a different variable called "drink" to an age-appropriate libation.
Loops
Another way you can control the order in which code is executed is with a loop. Where an if statement allows you to execute or not execute a certain block of code, a loop allows you to keep executing the same block of code multiple times.
There are several different types of loops, but the two that you'll find in almost any programming language are the while loop and the for loop.
A while loop works a lot like an if statement. You attach a Boolean (true or false) statement to the while loop, and as long as that statement is true, the loop keeps repeating. Basically it says "as long as this statement is true, keep going." As a consequence, something inside the looping code has to make a change that could cause the Boolean statement to become false, or else the loop will never end.
For example, the following code will print out the word "hello" 10 times, then stop:
while(x < 10) {
x = x + 1;
print("hello");
}
Notice that we used the variable x as a loop counter, to control the number of times the loop runs. The other most common type of loop, the for loop, is basically just a while loop with a built-in loop counter. You tell the loop right away how many times you want it to run, like this:
for(int x; x < 10; x = x + 1) {
print("hello");
}
The part after "for" just defines a counter. It says "start with a number (integer) we'll call x, and keep looping as long as x is less than 10. At the end of every loop, increment x by one."
Functions
The most powerful way to control the flow of a program is with functions, which allow you to reuse code. Also called a subroutine, a function is a block of code that you've given a name to, so you can reuse it any time you want.
For example, you could define a function called PrintHelloThenGoodbye by doing the following:
void PrintHelloThenGoodbye()
{
print("hello");
print("goodbye");
}
Then, if you called that function three times in your code, as follows:
PrintHelloThenGoodbye();
PrintHelloThenGoodBye();
PrintHelloThenGoodbye();
Your program would output "hello goodbye hello goodbye hello goodbye."
A function can also take variables as inputs, and
return an output value. So, for instance, you could write a function that takes a number as an input, and returns that number squared. It would like look like this:
int Square(int toSquare)
{
return toSquare * toSquare;
}
Notice the "return" keyword. That passes the following value back to whatever part of the code called the function. So, if somewhere else in the code we called the function like this:
print(Square(5));
The program would print out the number 25.
Syntax
Maybe the most intimidating thing about programming is the syntax—the strange punctuation marks and cryptic words that make a page of code look like a foreign language. Fortunately, in most programming languages, syntax is really only a couple of rules that you have to remember, and a lot of syntax is shared between languages.
It's all dependent on what language you're programming in, but here are a couple of syntactical elements that are common across many popular languages:
Semicolon The semicolon is like the coding equivalent of a period—each line of code ends with one. It's important, because in many languages, line breaks are just for readability, and don't have any effect on the execution of the code.
Parentheses Parentheses are used after functions (see above) to contain that function's parameters (or inputs). You might remember this usage from your high school math classes, when f(x) was a function that operated on the variable x.
Curly braces In a number of languages (particularly those derived from C), curly braces "{}" are used to enclose and group blocks of code. They're used, for instance, after the control structures described here (if statements, loops, and functions), to designate the block of code that the statement refers to.
Indentation Because all of the control structures can be nested inside each other, code tends to take on a sort of hierarchy. A particular line of code might be inside an if statement, which is inside another if statement, which is inside a loop that's inside a function. All that can get hard to keep track of! To make it easier, code is written with variable levels of indentation. The more indented a line of code is, the more deeply nested it is. In most languages, indentation is purely for readability, but in a few (like Python), it actually controls the grouping of code, and is used instead of curly braces.
Sample Code
This oh-so-practical program prints out an age-appropriate version of the song "99 Bottles of Beer."
function 99BottlesOfBeer(int age) {
var bottlesLeft = 99;
var drink;
if (age >= 21) {
drink = "beer";
} else {
drink = "coke";
}
while (bottlesLeft > 0)
{
print (bottlesLeft + " bottles of "
+ drink + " on the wall");
bottlesLeft = bottlesLeft - 1;
}
}
Click the next page to learn how you can take action with computer programming!
Advanced Course: Object-Oriented Programming
Taking a look at the bigger picture
Using only the tools we've discussed so far, you can write functions that manipulate variables in all sorts of ways—the foundation of pretty much any program you want to write. Unfortunately, as the complexity of a program increases, it becomes difficult to maintain code that's organized and easy to understand using only those concepts. As an example, if you were writing code for a bank to keep track of its customers' accounts, you would quickly end up with hundreds of functions and thousands of variables. It would become very difficult to understand what was going on in the code at any particular place, and more generally how the whole thing works.
That's what object-oriented programming (OOP) is for. OOP is a paradigm that allows you to group variables and functions together into classes, which are (usually) meant to model things or particular concepts. For instance, in the bank example, we might start by creating a class called "Account," which simulates a user account. Classes are made up of variables and functions (called methods when they're part of a class), so we start by figuring out what data (variables) and capabilities (methods) an account needs to have. For variables, we might use account number, the account holder's name, and the balance. For methods, we would want the ability to deposit money, which would increase the balance variable, and withdraw money, which would decrease it.
Once you've defined a class, you have to instantiate it for every object you're modeling. So in the bank example, we would create a new instance of the account class for every customer of the bank—that way every person can have his or her own account number and balance.
It all sounds very complicated until you get to play around with it yourself, but the basic idea of OOP is that we set up a system of tens, hundreds, or thousands of objects that can cooperate with each other to produce the effect that we want.
Object-oriented programming is not the only programming paradigm in use, but it is the most common. Understanding the core concepts of classes, objects, and methods is the last hurdle to programming in languages like Java, C#, and Python.
Educate Yourself
Learn to code at your own pace with these great online resources
We've talked a bit about semantics, syntax, and structure, the three things you need to write code. If you were able to follow along, you already know enough to start writing simple programs, and you can pick up the rest as you go. If it still seems a little murky, don't worry—programming is the kind of thing that really only clicks when you try it yourself. Here are some tips for getting your feet wet:
Use CodeAcademy
CodeAcademy is the best resource there's ever been for complete beginners to learn coding. It's a series of interactive tutorials that teach you the fundamentals of programming, one bit at a time. In each lesson, you'll write actual code that compiles and runs right in your browser, and the lessons build on each other gradually enough that you'll rarely feel out of your depth.
You can learn a number of languages at CodeAcademy, including JavaScript, Python, and Ruby. It won't teach you everything you need to know to be a professional coder, but it will give you the basic familiarity with the language that you need in order to start learning more complicated concepts.
Use Stack Overflow
Once you've gotten started with a language, the programmer Q&A site StackOverflow.com is the best repository for answers about more complicated topics. Don't start asking questions right away (someone has almost certainly asked about anything you're running up against), just use the search function to find answers related to any problems you have.
It's not the most newbie-friendly site on the web, but Stack Overflow is an unparalleled resource for programmers.
Use Google
Of course, Google is great for solving almost any sort of problem, but it's especially good for issues related to programming. Maybe it's because the people who tweak the Google search engine are programmers themselves, but Google is excellent at picking out relevant pages from various programming languages' documentation.
Ultimately, the key to learning to program is to not let yourself get overwhelmed. Hopefully, the concepts we've covered in this article have been enough to pique your interest, but don't worry if it's still a little confusing. Take your time, make use of the online resources available to you, and you'll have conquered the final frontier of PC power-use before you know it.
Next Steps
Two ways you can get started making something cool
Unity
If you don't pay much attention to the game-development scene, you might never have heard of Unity, the game engine that's quietly revolutionizing indie development. What's so good about it? Two things: First, Unity is a flexible and powerful engine for making 3D and 2D games. Unity takes care of all the low-end graphics and physics processing, so you can focus your coding energies on the high-end gameplay decisions. You can code in Java-Script or C# in Unity, and it can automatically build your game for you on almost any platform, from the PC to the PlayStation to the iPhone.
The free Unity game engine combines a drag-and-drop 3D interface with JavaScript and C# scripting.
Second, and perhaps more amazingly, Unity is available to everyone for free. Where previously a high-quality game engine would have to be licensed for tens of thousands of dollars, Unity lets you code professional-quality games for free. There are a few features that you have to pay for, but the free versions should still have all the tools you need.
To get started with Unity, visit www.unity3d.com and download the free IDE. There are plenty of great resources for learning to use Unity online, and the IDE comes with an extensive sample project and tutorial.
Arduino
If physical projects are more your thing, you can write programs that control devices in the real world, using a microcontroller like Arduino or Raspberry Pi. These microcontrollers feature small, inexpensive processors and can be programmed from your computer. By wiring the microcontroller to electronics including motors, sensors, and lights, you can build anything, from a robot to a sous vide machine
An Arduino board features a microcontroller chip, along with input and output ports to hook it into any project.
Arduino in particular has an excellent collection of documentation and tutorials. You can find a basic Arduino UNO board at Adafruit.com, Sparkfun.com, or Makershed.com for as little as $25-$30, and you can download the IDE (which comes with a whole load of sample scripts) at www.arduino.cc. The IDE uses the C programming language, which is more difficult than JavaScript, but the documentation is good and the actual programming required for Arduino projects tends to be very straightforward.
So, what are you waiting for? Get out there and start making something!