Computer Programming Basics

What Is Programming?

When you pick up your mobile device and respond to a message or comment on a friend’s picture, you’re experiencing the results of what someone programmed your phone to do. Programming is converting ideas into instructions that a computer can understand and execute. These instructions are specific and sequential. You can think of it a bit like a recipe. Let’s say you wanted to bake a cake. You would first need a list of ingredients and then instructions. We have flour, sugar, eggs, milk, and so forth for our ingredients. If you’ve ever baked blueberry muffins, you know that the amount of each ingredient can dramatically affect the outcome. If you accidentally use a full cup of milk instead of 1/2 a cup, you’d have a runny mess on your hands. Or what if you swapped the baking soda for salt? The muffins would never rise.

Similarly, the instructions we provide to computers must be specific to get our desired result. Computers are very literal. They try to execute our commands exactly. We might cause bugs or even crash the computer when we give them the wrong instructions. In computer programming, a bug occurs when something unexpected happens, whereas a crash occurs when your program stops early or freezes. The other characteristic of computer instructions is that they’re sequential, just like the steps in a recipe. The order of the steps will impact your final product. If we mix the baking soda before the eggs, we can cause our muffins to be dense, or adding the milk with the butter may cause the dry ingredients to clump and leave us with a lumpy result—the order of execution matters. You can say the same thing about programming. It’s all about giving computers the proper instructions and the correct sequence of steps to produce the desired result. The correct instructions are essential because programming is just as much about finding errors and preventing crashes as it is about writing the instructions or code the computer understands. So, to recap, programming is how we communicate with the digital world. It’s characterized by having instructions that are specific and sequential. And when we get our code right, just like in a recipe, the result is a program that helps us to accomplish our goals.

What Is A Programming Language

When you want to provide instructions to a computer, you use a programming language. There are hundreds, if not thousands, of programming languages available, each made up of its syntax, rules, semantics, or meaning. Let me give you an analogy. In English, we would write, Welcome, and use an exclamation mark, but in Spanish, we would write, Bienvenido. Semantically, they mean the same thing, but Spanish, unlike English, requires an exclamation mark at the beginning and end. This is one rule, or syntax, of the Spanish language. Likewise, programming languages also have unique syntax rules. Let’s look at a few examples of the Hello, world program. Maybe you’ve already heard of it before. It’s the most basic program used for decades to get started with a new programming language. It helps to highlight the differences in syntax between languages. First, we’ll look at a language called C++.

// Your First C++ Program

#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

The code above is how we would get the program to display “Hello World!” when run. Now, it may look strange to you, but that’s okay. Soon you’ll be able to understand line by line what’s happening. For now, let’s notice the syntax used in the language. It consists of double quotes around the words Hello World, the use of a semicolon at the end of statements, and there’s a return keyword right before a closing curly brace. All of these things are part of the syntax, or rules, of the C++ programming language. Let’s look at a few more examples. This language is called JavaScript.

// the hello world program
console.log('Hello World');

It’s a modern language used on virtually every website these days. The code above is how we would display Hello World using JavaScript. Notice the use of different syntaxes. First, the words ‘Hello World’ are now inside single quotes. We don’t have any curly braces. Instead, we see something about console.log. And finally, here’s ‘Hello World’ in Python.

print("Hello, World!")

Similar syntax to JavaScript, but now we don’t see any parentheses, and the use of double quotes has returned, just like we saw with C++. Now you may wonder, what’s the point of having so many different languages, especially if they do the same thing? Why not just have one programming language, and we all use it? The main reason why we have so many languages is that each comes with its own set of strengths and weaknesses. Some are ideal for programming small devices with limited memory, whereas others were made to handle complex mathematical computations. Regardless of the language you choose, a compiler must ultimately break down that language into the only one that computers understand, machine language. Machine language is highly complex for us to write because it’s mostly just a series of numbers. Trying to read machine language would take you forever, which is why we have these other languages that we call high-level languages. They’re closer to human languages, comprised of keywords, structure, and syntax that’s easier for us to learn and understand.

How To Write Source Code

Writing source code is what programmers do all day. It’s the instructions we have for the computer, and it’s written in plain text. In other words, we write it without special formattings, like bold, italic, or different font types. It’s mostly just the actual characters. Special formatting means that word processing applications aren’t suitable for writing code because, by default, they insert bits of information in files that prevent them from being plain text. Instead, you can use a text editor. If you’re on a PC, you already have a text editor called Notepad. Mac users have a program named TextEdit. Both applications allow you to write source code. Let’s look at an example.

We’ll go ahead and open up TextEdit, and the first thing you’ll notice is the formatting bar. By default, TextEdit stores files in Rich Text Format. That means you can do things like bold or italics or even underline. But this is not suitable for writing source code. To change it, we’re going to go over to TextEdit, choose Preferences, and then we’re going to select plain text under the Format section. If you’re on a PC using Notepad, you don’t have to worry about this. It uses plain text by default. So let’s start by doing our favorite program, Hello World. We’re going to do the Python version.

We type print, open parentheses, double quotes, Hello World, exclamation mark, double quotes, and a closing parenthesis. We did it. We’ve written some source code. Source code can either be one line, like you see here or thousands. It just depends on the needs of your program. Let’s go ahead and save this file. By default, TextEdit saves files with a .txt extension since it is plain text. But it’s best practice to use an extension corresponding to the programming language inside your file. Since we wrote this code in Basic Python, we will use the Python extension, which is .py. So let’s save this as helloworld.py. Each programming language has its file extensions. JavaScript uses .js, Perl uses .pl, and Kotlin uses .kt. Now that we know how to write our code, we need to learn how to translate it into the language the computer speaks called Machine language. Turning source code into machine language is vital to getting it running on any device.

Running Code

If we were to double-click our Python file, it would just open in an editor, but it wouldn’t do anything with the code we put inside. Why is that? Well, it’s because Python is an interpretive language. An interpreted language means we have to interpret it into machine code and haven’t run the interpreter yet. There are three main ways to translate source code into machine code: compile it, interpret it, or a combination of both. Compile, interpret? Let me give you a real-world example. Say you need to write a letter to someone who speaks Spanish but doesn’t speak English. What would you do? You could first write it in English, then use a translation service to convert your letter into Spanish and mail it. The other person would never see your original letter, only the one in Spanish. That’s what compilers do. They take your high-level programming language and turn it into an executable that contains low-level machine code. This way, users can run your code on their machine without needing your source code. The second option for sending a letter is to send it to them in English. Then they could have a friend who speaks English read each line of the letter first and tell them what it means in Spanish. Every time our Spanish friend needs to read the letter, the interpreter will need to return and reinterpret the letter. Interpreters work like this: they process your source code each time it’s run, line by line, and it’s up to the other user to have the needed interpreter available on their machine. Now, this is an oversimplification of both compilers and interpreters. They perform complex functions that help to optimize the size, speed, and security of your source code, but it helps to convey the general idea of how they differ. Typically, languages like C, C++, and Objective-C are known as compiled languages, whereas PHP and JavaScript are known as interpretive languages. And finally, Java, C#, and Python use the combination approach. Nowadays, the line between compiling and interpreting is a bit blurry, as many languages have some variants of both. The critical thing to remember is that you may need to perform additional steps to get your code to run on your or someone else’s machine.

What Is An IDE

You can write entire programs in simple text editors, but in practice, few programmers do. They use enhanced text editors called integrated development environments, or IDEs. IDEs provide features that speed up code development. What tool would you use when you want to write a letter, an essay, or a resume? You’d probably use a word processor such as Microsoft Word or Google Docs. They have special tools that help you check grammar and spelling and add nice formatting. For programming, we use an IDE in the same way. An IDE is an application that provides the special tools needed to write, debug, and compile code. Let’s take a look at two popular IDEs to see what they have to offer. Don’t worry about downloading anything right now. We’re just going to check them out together. We’ll start with Xcode. Xcode is used to develop applications for Apple products like the iPhone, iPad, or Mac. You can see a difference between our simple text editor and a more sophisticated IDE.

We have line numbers on the left side of our source code editor. Line numbers allow us to reference individual statements in our code. There’s also the use of color, known as syntax highlighting, to aid in pointing out essential keywords and improve the readability of our code. And finally, notice how you can preview what your code will look like when compiled and run on an actual device—a great tool to improve your coding process. Now, although Xcode is excellent, let’s look at one more example, Visual Studio Code.

Visual Studio Code, or VS Code for short, is a more lightweight editor than Xcode, and it was initially designed for scripting languages, such as JavaScript and TypeScript. However, it gradually came to support many more languages through powerful extensions. You’ll find developers using it to develop apps in dozens of languages. One of its unique features is called IntelliSense. IntelliSense allows you to get code suggestions while you’re typing. It works like auto-complete when sending a text or searching in Google, and it guesses the rest of the phrase you’re typing. And unlike a simple text editor, we can run our Python code from within the IDE. It’s cool. And there are many more IDEs out there. My favorite is PyCharm since it is the best Python editor available.

Computer Programming Summary

  • The .js file extension is used If you are writing your source code in the JavaScript programming language.
  • The standard extension to use for a Python source code file is .py.
  • The Xcode IDE can be used to develop an application for an iPhone.
  • Intellisense is an IDE feature that allows you to get code suggestions while typing.
  • A text editor can be used to write source code.
  • C++ is a compiled language.
  • JavaScript is considered an interpreted language.
  • Source code is written in plain text.
  • Programming can be defined as converting ideas into instructions that a computer can understand.
  • Computer instructions need to be sequential because the order in which these instructions are executed is important.
  • In programming, a crash is when your program stops early or freezes because something unexpected happened.
  • There are many programming languages to address many different computing needs.