Fabio Berger

Intro to Programming

Introduction

Welcome to the wonderful world of programming. In this intro guide, I want to help you get up to speed with some of the fundamentals of what 'programming' actually means and how you can get started with learning this very powerful tool. Computers can empower you to build systems that work while you sleep, send information around the globe in milliseconds and calculate really complex equations in the blink of an eye. Let's get you some of those magic powers so that you can apply them to solve the problems you care about.

Everything is a file

Computers are simply a collection of files. Some files, such as Microsoft Word files, contain the words we humans write. They also contain the instructions on how to display those words on the screen. What to bold, underline and what to make font size 24 and what font size 28. Similarly, every computer program is written in files, using characters found on your keyboard. Look down at your keyboard now. Do you see all those weird keys on the perimeter of your keyboard that you hardly use? (i.e #, %, &, *, [, ], |) Lots of those are used when writing computer programs and have been assigned specific meanings in computer languages. For example, the character ! has been assigned the mathematical operation of negation. If I were to write: isSheGerman = !true that would mean the same as isSheGerman = false. Not true is false. But we are getting a little carried away here, what is important to realize is that computer programs are not written as 1001101001000101011 by programers but rather are written in english looking words in files. You can then tell the computer to run the file and execute the instructions within them.

A program is a fancy word for recipe

Every program is simply a set of instructions that the computer reads from top to bottom. They are simply a recipe of what to do and when to do it. Let's write out a simple recipe for making bread and then see what the equivalent computer program would look like:

Recipe

- Get a large bowl
- Add yeast and warm water to the bowl
-  Add 3 tablespoons of sugar
-  Add 1 tablespoon of salt
-  Add 2 tablespoons canola oil and 3 cups flour
-  Beat 30 times
-  knead until smooth and elastic
-  Cover and let rise for 1 and a 1/2 hours
-  Bake at 375° until golden brown

Program

var largeBowl = [];
largeBowl.push(yeast);
largeBowl.push(1TablespoonSugar * 3);
largeBowl.push(1TablespoonSalt);
largeBowl.push(1TablespoonCanolaOil * 2);
largeBowl.push(1CupFlour * 3);
for (ingredient of largeBowl) {
	beat(ingredient);
}
while(!isWellKneaded && !isElastic) {
	knead(largeBowl);
}
setTimeout(bakeAt375DegreesUntilBrown(largeBowl), OneAndAHalfHours);

This is what you are writing when you are programming. Just like a human following a recipe, the computer will read these instructions written in a language it understands and will follow each instruction sequentially. This example is in javascript, which most websites are written in, but a lot of the concepts you learn in one language are the same in every other language.

3 Basic Concepts Found In All Programming Languages

Although there are many programming languages: python, javascript, golang, ruby, C++, java, objective-c, etc..., all of them have the following basic building blocks to express the instructions we humans care about. Here are some of the main concepts you will be learning about and why they are important:

Variables

Just like in math, sometimes it can make life easier to give a name to values instead of simply re-writing the number everywhere. In math you might say:

x = 10
y = 5
z = 2*x + y

Therefore the value of z is 25. Similarly, you can assign values to variables in programming. A value can be a number, but it can also be text or a collection. Here are some examples:

var x = 10;
var y = 5;
var z = 2 * x + y;

z is equal to 25!

var firstName = "Fabio";
var lastName = "Berger";
var fullName = firstName + " " + lastName;

fullName is equal to 'Fabio Berger'.

var pets = ["cat", "dog"];
pets.push("parrot");

pets is equal to ["cat", "dog", "parrot"].

In programming, the = sign does not mean equality in the mathematical sense, but rather assignment, let us set the value of the name on the left to the value on the right. Variables are important because they let us give more descriptive names to the data we are processing and can make our recipes more readable.

If Statements

An if statement is a conditional statement. Sometimes just like in a human recipe, you want to do something IF something else has happened. For example:

if (ovenIsAt360Degrees) {
  putBreadIntoOven();
} else {
  wait30MinutesAndTryToPutBreadInOvenAgain();
}

When the computer needs to decide whether to put the bread into the oven, we can tell it to only do so IF the oven is already pre-heated to 360 degrees celsius. Therefore if statements let us have divergent paths of instruction for the computer.

Loops

Sometimes you want to do something to every item within a collection. Let's say you have 6 guests at a party, we want to give each one a drink and then sit them down. To do this, we can use a for loop:

var guests = [fabio, john, marlene, soroush, alex, stephanie];
for (guest of guests) {
  giveADrinkTo(guest);
  sitGuestDown(guest);
}

This means, for every guest in the collection of guests, give them a drink and then sit them down. The instructions between the curly braces will be executed once for each guest at the party. Each will be given a drink and then sat down. This lets you write the instructions only once even though you want them performed for however many guests show up to your party.

Conclusion to introduction

I hope this very short introduction has given you the sense that when you pop open the hood of your computer and get a glimpse of how they work, that some of the mystery that surrounds them disappears. Programming is an exercise that requires wit, problem solving and creativity. It also requires a lot of patience and a tolerance for making mistakes. The reward however is mastering a tool that can be used in every single field of human endeavor. Stay tuned for more tidbit lessons and check out some of these more comprehensive online resources to continue your journey into the world of programming! ;)

*Last piece of advice: When you start learning to program, many things will not make sense the first time you see/read them. Don't get discouraged, just keep reading, even if you don't understand and somehow your brain will eventually start to make sense of it!