Published: Sat Feb 12 2022 - Avg Reading Time: 11min

Intro to programming fundamentals


In my journey to become a self-taught web developer, I have always wondered what I missed out on by not doing a CS degree? After some conversations with my fellow co-workers and other CS students, I realised what it was I missed out on. Language Agnostic programming fundamentals, what that means is to learn and understand the fundamentals of Computer Science(programming languages) out of the context of a specific language like JavaScript, C#, C++, Python etc.

Why learn like this? mistakes a lot of self-taught developers make is to learn a very specific set of skills, like HTML, CSS and JavaScript to solve a very specific set of problems like web development. While there is nothing wrong with this, it does pose some problems in the future. What if you need to switch your language? Will you be able to adapt quickly despite never having studied the new programming language? So to solve this it is very important to understand and learn these fundamentals.

So I am going to try my best to come up with a simple and quick overview of the programming fundamentals that you can refer to anytime.

What is programming

The technical definition of programming is a few instructions used to illustrate the basic output rules of a programming language. A programming language is a set of keywords, symbols, identifiers, and rules by which programmers can communicate instructions to the computer. They follow a set of rules called syntax. Each programming language follows specific syntax rules, although some share the same rules.

How do we write programs

So to give a computer a set of instructions we need to write it down somewhere, We can’t just use plain alphanumeric words either. So what we need is something called an IDE (Integrated Development Environment), an IDE gives us tools to write, debug, and convert code into machine code so the computer can understand it. Now each language has its own rules or syntax and if these rules are broken it will result in an error, similar to learning a spoken language like French or English, we need to follow the rules of the language for other people to understand us. There are some IDE’s that are built for specific languages, there are some that can be used with any language and offer built-in support or extensions to help you write code.

Variables

Variables are the building blocks of programs since every single program ever will use variables it is important to understand how they work. A simple explanation is to think of variables as a box, this box can hold anything you want it to hold, but it can’t mix different things each box must hold only one unique thing. But what if I have 20 boxes how will I know what is inside each box? What we can do is give each box a unique identifier (name) so we can refer back to that box.

So how does this look practically?

Well, let's go ahead and create a new variable.

myName = 'Dylan'
Enter fullscreen mode Exit fullscreen mode

So how does this look practically? Well, let's go ahead and create a new variable. myName = 'Dylan' So as you can see we now have a new variable. The value of This variable is ‘Dylan’ but we can’t refer to this box i.e. variable by its value, so we gave it a name i.e. a unique identifier. So if someone says they want the value that's inside the ‘myName’ box I can quickly find this box open it and see the value is ‘Dylan’.

So how does a computer do this? Well a computer has memory and this memory has spaces to store information, each space has a unique number predefined when the hardware was built. So when you declare a new variable the computer assigns this variable to a specific point in memory now it’s not efficient for us to memorise our computers memory locations so we assign it a name that we (humans) can refer to and the computer can use to know where to look for the value.

What type of information can variables store?

The first types we will learn about is primitive types, note that each programming language has its own unique primitive data types and some language share similar ones, here are common ones found in most languages:

  • String
    • In any computer programming language, a string is a sequence of characters  used to represent text.
    • Example: “This is a String”
  • Number/Integers
    • Number is a numeric data type that represents a non-decimal number like 1 or 432 or 9584939
  • Floating-point-numbers
    • A positive or negative whole number with a decimal point . For example, 5.5, 0.25, and -103.342 are all floating point numbers
  • Booleans
    • Boolean is a logical data type that can have only the values true or false
  • undefined
    • Undefined are automatically assigned to a variable if you declare a variable without assigning it a value.

Conditional Statements

Let’s say you want to write a program to do something only if a certain condition is met, how would you do that?

Well with conditional statements of course.

if myName equals 'Dylan' then doSomething()
Enter fullscreen mode Exit fullscreen mode

The above example is a conditional statement, we want to doSomething but for that to happen variable myName must equal ‘Dylan’.

So what are the types on conditional statements and how do we use them ?

Conditional statements are useful to create the control flow in your application through the use of conditionals. Conditional statements can get quite complex so here I created a visual representation for you to get an idea about how and why I might use it.

programming work flow

In the above example, we created a function that checks if Variable A equals a certain colour and then handle it depending on whether it passes our checks. We can pass almost anything into the conditional weathers is a statement, a function or a comparison as long as it equates to true or false.

  • If then Statements

    • The if statements common across many programming languages. Although the syntax varies from language to language, the basic structure (in pseudocode form) looks like this:
    • If the value passes it will execute the code inside the statement.
    • If it fails it will skip over the code and continue on with the rest of the program
    If stock=0 Then
        message= order new stock
    End If
    
  • Else If

    • Else if are similar to if statements with the exception that it has two way execution. Meaning they handle the false exception when testing a condition.
    If stock=0 Then
        message= order new stock
    Else
            message= stock is available
    End If
    
  • Case and Switch

    • ◦ Let's say we have a list of cases we want to check and each case has its own statement to execute, now writing a long nested If-Else-If statement will work it's not efficient and it's not maintainable. This is why we would use Case and Switch statements.
    • Switch statements take action according to a list of constant matches, if it doesn't match it has a fall-back or default action to take instead in some cases the default action can be omitted.
    • As you can see we pass a variable to the case statement if it matches a, b or c then it will execute the corresponding code, if it doesn't match at all it will revert to the default action.
    case someVariable 
        'a' : handleA()
        'b' : handleB()
        'c' : handleC()
        default handleDefault()
    

Arrays

Think of arrays as a big rack to stack boxes, remember the box analogy for variables? An array can hold multiple or no boxes at a time and each box can contain a variable, but there are some rules to this rack.

  1. Each box can’t have a have a unique identifier.
  2. The rack has to be filled from top to bottom
  3. there are unlimited space on this rack (or as much as your computer’s memory allows it to have)

If each box has no name or identifier, how can we know what each box has or refer to a specific box? Well, it uses a number index so remember the rule you have to fill each array from top to bottom, that is because each space in this array has an index number to identify the spot of the box. So, for example, I can say get me box 25 and all we do is go straight to box twenty-five to retrieve its value.

Here is the catch, arrays start from an index of 0 not 1, so position 1 has an index of 0 and position 2 has an index of 1 and so forth. This is called zero-based indexing

Examples:

So here we have an array of numbers from 1 to 8. How do we find number 5? we have to go to box 4.

myArray = [1,2,3,4,5,6,7,8];
Enter fullscreen mode Exit fullscreen mode

How do we do that? we use Zero Based indexing.

//get number 5
myArray[4]
Enter fullscreen mode Exit fullscreen mode

Using this method we use brackets and pass a number to get the index of passed n value.

Types of arrays

  • One-Dimension Arrays

    • A one-dimensional array (or single dimension array) is a type of linear array.
    myArray = [1,2,3,4,5,6,7,8];
    
  • Multidimensional arrays

    • Think of these as non-linear or nested arrays, arrays within arrays.
    • There is no limit to how deep arrays can be nested
    myArray = [[1,2,3],[4,5,6]];
    
    //similary we access these arrays exaclty to how we access linear arrays
    myArray[1][0] 
    // equals 4
    

Element identifier and addressing formulas

So in the above example, we access arrays using the zero-based indexing
 although that is not the only way to access arrays:

  • 0 (zero-based indexing)The first element of the array is indexed by subscript of 0. ****
  • (one-based indexing)The first element of the array is indexed by subscript of 1.
  • (n-based indexing)The base index of an array can be freely chosen. Usually programming languages allowing n-based indexing also allow negative index values and other scalar data types like enumerations, or characters may be used as an array index.

Using zero based indexing is the design choice of many influential programming languages, including CJava and Lisp. This leads to simpler implementation where the subscript refers to an offset from the starting position of an array, so the first element has an offset of zero.

Loops

Loops are what the name implies, a task that gets repeated several times or until a condition is met.

For example:

Say I want to print the number 1 to 1000, now writing 1000 lines of code is inefficient, so we use a loop instead to save time.

Type of loops

  • For Loop

    • Is a loop that runs a fixed amount of times.
    • Here is a basic pseudocode for loop
    from 1 to 20 
        print('hello world')
    end
    
  • While Loop

    • Is repeated as long as the passed condition is true
    • Here is a basic pseudocode for a while loop, so if myName changes the loop will stop executing
    while myName= 'Dylan'
        Print('Dylan is his name');
    end
    
  • do while loop

    • is repeated until a passed condition is false
    • Here is a basic pseudocode for a do while loop, note even if myName is never equal to ‘Dylan’ it will run at least once.
    do
        Print('Dylan is his name')
    while myName= 'Dylan'
    
    

Loops can serve many functions, besides just repeating tasks, loops are the main function we use to manipulate, create or do something with arrays

For Example:

Say I have a array of numbers like so:

myArray = [1,2,3]
Enter fullscreen mode Exit fullscreen mode

Now I want to add 2 to each number, ok we can do this.

myArray[0] = myArray[0] + 2
myArray[1] = myArray[1] + 2
Enter fullscreen mode Exit fullscreen mode

Now see the problem here? what if we have an array with the length of 10 000? Well this is where loops come in.

//lets loop over each item in the array and add 2 in pseudocode
myArray.foreach(item) 
    item = item + 2
end

Enter fullscreen mode Exit fullscreen mode

In the above example, we use a for each loop to go over each item in the array and change that item to its current value + 2. Note the above example is very basic and each programming language will have its own method of handling loops with arrays.

Errors

So errors are a part of any programmers life and you can expect a lot of them once you start writing code. Now there is no need to be scared of errors and don’t worry the chance of you breaking stuff is very unlikely.

Types of errors

  • Syntax Errors
    • This is when there are parts of the program where you fail to follow the rules of the written language.
    • This one is the easiest to solve because our IDE’s usually point these out to us.
  • Runtime Errors
    • These errors only happen once you run the code
    • This is caused by a part of your code not being able to be computed
    • A most common Runtime error beginners run into is infinite loops
  • Logic Errors
    • Logic errors happen when our program runs smoothly but the expected result is not what we wanted.
    • These are often the hardest errors to try and solve and something beginners can really get stuck with.
    • Strategy to overcome these is incremental programming, this means to often test your program and not once all the code is written to ensure we debug as we go, not once we done this will help you identify logical errors early on.

Functions

Functions are organized blocks of code that we can reuse in our program without having to rewrite the same thing over and over again. This is called modularity. Depending on the programming language, a function may be called a subroutine, a procedure, a routine, a method, or a subprogram. Using functions can allow you to be able to keep your code clean and organized, making it easy to read, and allowing the debugging process to be easier.

A function can manipulate an existing variable or value or create a new value and return that value.

Look at an example, Say we want to get the number of characters in someone's name..

We can do something like this:

myName = 'dylan'

newArray = myName.split() //we assume split is a built in function to split a strings characters into and array.
//newArray = ['d','y','l','a','n]

//get the length of the array
charLength = newArray.length() //we assume .length() is a built in function to get the length of an array
//result = 5
Enter fullscreen mode Exit fullscreen mode

Ok so this works but now image if I have to retype the above code 29 times, that's inefficient and can cause debugging headaches. Instead let’s create a function.

function getStringLength(value)
    array = value.split()
    return array.length()
end

charLength = getStringLength('Dylan')
//result = 5
Enter fullscreen mode Exit fullscreen mode

o in the above example, we create a reusable block of code using the function keyword, passed it a value, used that value to create an array to get the desired result. Then used the return keyword to give us back the length of the array. Now we can call this function whenever we feel like and wherever we feel like without having to rewrite the logic.

Side note: each programming language has its own rules for writing functions the above code is just an example and not language-specific.

Objects/Dictionaries

Objects or [Object-oriented programming (OOP)](https://en.wikipedia.org/wiki/Object-oriented_programming) is probably the most powerful tool programmers have. Beginners can sometimes struggle to get their head around OOP concepts but once it clicks it can make you a very powerful programmer.

Objects can contain data and code, data in the form of key and value properties and code in the form of functions that sit within the object known as methods.

We won’t dive too deep into object methods but let’s get a good look at how the key and value properties work of objects.

Example:

myObject: {
    name: 'Dylan',
    surname: 'Britz',
    favourite_foods: ['meat','fish','vegetables']
}
Enter fullscreen mode Exit fullscreen mode

So above we have a simple object, there are a few things I want you to note, the key properties: name, surname and favourite_foods and the corresponding values. So think of this object similar to the array rack analogy, except this rack has no shelves only a frame, to add something to this rack we need to add a shelf(key) with a unique name so we can refer to it later on and add a value that sits on the shelf. Now, these values can be any primitive data type, even a function(refer to as a method).

Conclusion

Thanks for taking the time to read, I hope this broad overview helped you getting a fundamental understanding of the building blocks of programming, and if you want to receive more articles like this make sure to subscribe.