The main concepts of programming languages

The main concepts of Programming Languages
In this course you’re going to learn a few concepts that are key to every programmer.
In the end you will have a clear understanding of:
What is input, and what is output
What are variables
What are loops
How we can alter what the program does based on the state of the program, using conditionals
How we can divide the program in little subprograms using functions
How we can divide the program into separate files
What are types, and why they are useful
What are objects

Input and output

A computer has one job. Wait for an input, do something (what we tell them, hopefully!) and return an output.
Yes, looks like they do a lot more. But if you bring things to simple terms, it’s all input and output. You open a program - you gave the input with a mouse click, or the command line. You click a button. You press a key. You .. do things. The computer runs a program that is built to handle that input.
And the computer returns a result. Something moves on the screen. Words. Pictures. Things happen.
Internally, a lot happens between the time you give the input, and the time you receive the output. Which might be just a few milliseconds, but code (as we know) is executed really, really fast.


A variable is a way to store values into your programs, and to refer to them using meaningful names.
Suppose you have the number “5” and you want to give it a special meaning.
For example, the age of a child.
You can assign it to a variable. The exact syntax depends on the programming language, but in general terms you can imagine it being
childAge = 5
In JavaScript for example you would use
let childAge = 5
(read it as “let childAge be 5”)
Now you can use the childAge variable, which has the 5 value assigned, in other operations. Like printing:
print childAge//this prints "5" to the screen
(I’m still using pseudocode)
Now, a variable can typically change its value.
If it’s the child birthday, you can change the value to
childAge = 6
Variables are an essential part of programming.
One thing that’s very important is naming your variables in the right way. As with many things, there’s no “one true way” of doing it. It also depends on your team, if you will work on a team. But one thing that is more or less universal is that it’s important to give variables a meaningful name, so that when you look at them in your programs, you can infer their meaning while you read the code.
childAge is a good example - it’s the age of a child.


Programmers are lazy people. Instead of repeating a task 10 times, they rather spend some time up front, create a procedure that can be repeated, and run that 10 times.
This can be done on several levels. By creating automated tasks for your day to day activities, for example.
When you do this inside your programs, that’s what we call a loop.
Here’s an example of a loop. It’s written using JavaScript:
const list = [1, 2, 3, 4, 5]for (let value of list) { console.log(value)}
This loop will run 5 times, exactly like the elements found in the list array, and will print the content it finds. The loop will automatically end when the list ends.
Some other kind of loops available in JavaScript do not have a fixed end like this. It’s the case of the while loop, which always checks for a condition before running the loop:
while (true) { //do something}
In this case the condition is always true, so the loop will run forever. If you had
while (false) { //do something}
the loop would never run, and the control goes to the instructions you give next.
Using loops you can usually decide to break when a particular situation happens.
In JavaScript this can be done using the break statement:
const list = [1, 2, 3, 4, 5]for (let value of list) { console.log(value) break}
This will break the loop at the end of the first execution, so instead of printing all the 5 numbers, it will only print one.
Loops can be tricky at times, but they are highly useful.


At some point in any program (as in life) you have to face a choice.
This is an essential part of programming.
Did the user type a number or a letter? Was it the number 1 or a number bigger than 5? Is this an image or a PDF file?
We call those conditionals. There is code that will only run if a condition is satisfied. Other code will not even run, if a condition is not met.
We wrap those portion of codes into blocks. In JavaScript, blocks are identified by curly brackets.
The most common conditional is if. Available in most languages under this name, it verifies a condition before running the code contained in its block:
if (2 > 1) { //run this code}
This is a simple example that always executes. Because 2 is bigger than 1. Usually you compare variables:
if (a > b) { //run this code}
After an if statement you can link an else statement, to execute code if the if condition is not met:
if (a > b) { //run this code} else { //run this other code}
Those blocks can be nested, to create more complex scenarios:
if (a > b) { if (c > 5) { //run this code } else { //run this other code }} else { //run this other code}
Another conditional statement in JavaScript is switch. Compared to if, it allows for an easier definition of multiple choices:
switch (a) { case 1: //do something break case 2: //do something break case 3: //do something break default: //eveything not listed as a case break}
I mention JavaScript to give you the real syntax that you’re going to use, but those are all general constructs that you will find in many different languages, maybe slightly changed but more or less equivalent.
Another conditional in JavaScript is the ternary operator ?: which allows a more terse syntax:
isTrue ? /* do this */ : /* do that */
We’ll learn more about this, and the other conditionals, later on.


With variables, loops and conditionals you are already well equipped to create cool little applications.
Once you grow a certain point, however, it’s hard to keep things manageable. When you write down 100, 200, 500 lines of code, it’s all fun. But go past that number and you need a way to organize them.
Various programming languages have different ways to do so. JavaScript in particular offers one way using functions. Functions are not limited to JavaScript, of course - they are adopted into many others.
A function is a block of code with a name, which can be invoked and run when needed.
For example here’s a function:
function doSomething() { //code that is contained into doSomething()}
Now into another part of the program you can call doSomething() and the code contained into it will execute.
Functions can accept arguments. In this way they can encapsulate a lot of functionality that the outer program does not need. If a function just needs a variable value to perform a calculation or save data to a database or communicate through the network, then that variable is all it should get from the rest of the program.
Here’s an example
function doSomething(aVariable) { //code that is contained into doSomething() console.log(aVariable)}
Functions are great because they let you abstract your code, and reuse it in other places.
You can have multiple parts of a program share a common functionality into a function, so you don’t have to maintain a similar snippet of code in multiple parts of your codebase.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.