Programming is nothing more than writing step-by-step instructions for a computer to follow. If youve ever written down the steps for a recipe or scribbled directions for taking care of your pets while youre on vacation, youve already gone through the basic steps of writing a program. The key is simply knowing what you want to accomplish and then making sure you write the correct instructions that will tell someone how to achieve that goal.
Although programming is theoretically simple, its the details that can trip you up. First, you need to know exactly what you want. If you want a recipe for chicken chow mein, following a recipe for baked salmon wont do you any good.
Second, you need to write down every instruction necessary to get from your starting point to your desired result. If you skip a step or write steps out of order, you wont get the same result. Try driving to a restaurant when the list of driving instructions omits telling you when to turn on a specific road. It doesnt matter if 99% of the instructions are right; if just one instruction is wrong, you wont get to your desired goal.
The simpler your goal, the easier it is to achieve it. Writing a program that displays a calculator on the screen is far simpler than writing a program to monitor the safety systems of a nuclear power plant. The more complex your program, the more instructions youll need to write, and the more instructions you need to write, the greater the chance youll forget an instruction, write an instruction incorrectly, or write instructions in the wrong order.
Programming is nothing more than a way to control a computer to solve a problem, whether that computer is a laptop, smartphone, tablet, or wearable watch. Before you can start writing your own programs, you need to understand the basic principles of programming.
Programming Principles
To write a program, you have to write instructions that the computer can follow. No matter what a program does or how big it may be, every program in the world consists of nothing more than step-by-step instructions for the computer to follow, one at a time. The simplest program can consist of a single line, such as
print ("Hello, world"!)
Obviously, a program that consists of a single line wont be able to do much, so most programs consist of multiples lines of instructions (or code), such as
print ("Hello, world!")
print ("Now the program is done.")
This two-line program starts with the first line, follows the instructions on the second line, and then stops. Of course, you can keep adding more instructions to a program until you have a million instructions that the computer can follow sequentially, one at a time.
Listing instructions sequentially is the basis for programming. Unfortunately, its also limiting. For example, if you want to print the same message five times, you can use the following code:
print ("Hello, world!")
print ("Hello, world!")
print ("Hello, world!")
print ("Hello, world!")
print ("Hello, world!")
Writing the same five instructions is tedious and redundant, but it works. What happens if you want to print this same message a thousand times? Youd have to write the same instruction a thousand times.
Writing the same instruction multiple times is clumsy. To make programming easier, the goal is to write the least number of instructions to get the most work done. One way to avoid writing the same instruction multiple times is to organize your instructions using a second basic principle of programming, which is called a loop .
The idea behind a loop is to repeat one or more instructions multiple times, but only by writing those instructions down once. A typical loop might look like this:
for i in 1...5 {
print ("Hello, world!")
}
The first line tells the computer to repeat the loop five times. The second line tells the computer to print the message Hello, world on the screen. The third line just defines the end of the loop.
Now if you want to make the computer print a message a thousand times, you dont need to write the same instruction a thousand times. Instead, you just need to modify how many times the loop repeats:
for I in 1...1000 {
println ("Hello, world!")
}
Although loops are slightly more confusing to read and understand than a sequential series of instructions, loops make it easier to repeat instructions without writing the same instructions multiple times.
Most programs dont exclusively list instructions sequentially or in loops, but use a combination of both, such as
print ("Hello, world!")
print ("Now the program is starting.")
for I in 1...1000 {
print ("Hello, world!")
}
In this example, the computer follows the first two lines sequentially and then follows the last three lines repetitively in a loop . Generally, listing instructions sequentially is fine when you only need the computer to follow those instructions once. When you need the computer to run instructions multiple times, thats when you need to use a loop.
What makes computers powerful isnt just the ability to follow instructions sequentially or in a loop, but in making decisions. Decisions mean that the computer needs to evaluate some condition and then, based on that condition, decide what to do next.
For example, you can write a program that locks someone out of a computer until that person types in the correct password. If the person types the correct password, the program gives that person access. However, if the person types an incorrect password, the program blocks access to the computer. The following is an example of this type of decision-making:
if password == "secret" {
print ("Access granted!")
} else {
print ("Login denied!")
}
In this example, the computer asks for a password, and when the user types in a password, the computer checks to see if it matches the word secret. If so, then the computer grants that person access to the computer. If the user does not type secret, then the computer denies access.
Making decisions is what makes programming flexible. If you write a sequential series of instructions, the computer will follow these lists of instructions exactly the same way, every time. However, if you include decision-making instructions, also known as branching instructions, then the computer can respond according to what the user does.
Consider a video game . No video game could be written entirely with instructions organized sequentially because then the game would play exactly the same way every time. Instead, a video game needs to adapt to the players actions at all times. If the player moves an object to the left, the video game needs to respond differently than if the player moves an object to the right or does nothing at all. Using branching instructions gives computers the ability to react differently so the program never runs exactly the same way.
To write a computer program, you need to organize instructions in one of the following three ways, as graphically show in Figure .