A Basic Understanding Of Computer Programming

Recently, I was challenged by a friend to create a tutorial so basic, that even a very beginner could find a starting point in the world of engineering and computer science. So, I’ve decided to compile this document with my explanation of how a computer interprets and understands the random bits of text we throw at it. While this is not a complete tutorial, and I hope to add more to this in the future, I will be responding to comments asked on this post so please ask away. Through helping friends previously, I’ve learned that the best way to teach programming is through examples of how code can be used in the real world. For this post, I will be giving examples of how game developers use many of the systems and methods described.

What Is Computer Programming?

To begin, I should define computer programming. According to Wikipedia (I know, great source)

“Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task. Programming involves tasks such as: analysis, generating algorithms, profiling algorithms’ accuracy and resource consumption, and the implementation of algorithms in a chosen programming language (commonly referred to as coding).”

(Wikipedia Contributors, 2021)

This sounds far more complicated than it really is. Simply put, computer programming is the way a programmer communicates with a computing device in order to force it to perform tasks and operations for the programmer. Sometimes this involves shortening a task by having a computer perform the action for us, or save us time by having a program repeat over and over again instead of forcing us to do it each time.

What Are Variables?

Like in algebra, variables are used to hold data values while we code. We use these typically to store data that is expected to change. In most languages, a variable will be assigned a name and a value. If we want to reference that variable later in code, we need to use its given name.

What Are Base Data Types?

Computers don’t understand English, unfortunately. Instead, a computer understands inputs and outputs, and our code decides how those inputs and outputs should be processed. Data types are one of the most important concepts to understand for most programming languages. Data types are split into categories, we have base data types, that have simple manipulation strategies, and then we have more complicated data types with complex manipulation strategies. When writing our code, we must create variables, and data which are managed and processed by our methods, functions, and mathematics. Data types are the different types of data that can be processed by the code. In most programming languages we focus on the following data types:

  • Integers: Most typically referred to as an int, an integer is a data type that holds a number. This can be any whole number, positive or negative. A game developer may use this to hold a players level in a skill tree, or maybe the number of points they have.
  • Floats: A float is a “floating point” data type. This also holds a number, but it can be negative or positive and can hold any number of numbers after the decimal point. A game developer may use this type of data in many different physics equations, or also in a simple health bar, with a precise combat game.
  • Booleans: A boolean is a true or false statement. These are incredibly useful for loops, and conditionals. A boolean can be used by a game developer for determining if an enemy is dead or not, or checking if a player is touching the ground or not.
  • Characters: A character data type, also referred to as a char, is used to hold one single letter. This is helpful when assigning single characters to game objects or when creating keys for cryptography.
  • Strings: A string is a list of characters typically surrounded by quotations (“”). These are used to hold text and can represent numerous values, such as player names, ability names, and all value that can be represented with a word or phrase.

What Are Operators?

Operators are very important to many coding languages. They allow us to perform task between two variables. Not all base operators work on all variable types and some specialized variables have their own custom operators.

  • + : The addition symbol in many languages means to add two variables, with strings this concatenates them, merging them together, with integer this would add the two numbers.
  • : The subtraction symbol does the opposite of the addition symbol and takes the difference between two variables.
  • * : The asterisk represents multiplication and multiplies two variables, returning the product.
  • / : The slash or the division symbol, divides two variables and returns the quotient.
  • %: The percentage symbol represents modulus. Modulus returns the remainder when two values are divided.
  • = : The equals sign on its own sets a variable equal to a value.
  • != : This symbol checks whether two values are not equal.
  • == : Two equals signs checks if two values are equal.
  • >, <: These symbols represent the greater than symbol or less than symbol, determining whether two values are greater than or less than one another.
  • >= , <= : These symbols represent the greater than or equal to symbol and the less than or equal to symbol, determining whether two values are greater than or less than or equal to one another.
  • && : This is the logical and symbol, checking whether both statements connected by this symbol are true.
  • || : This is the logical or symbol, checking whether either of the statements connected by this symbol are true.
  • ! : The not symbol, checking if a statement is not true.

What are If Statements?

If statements are ways of checking whether or not certain things have occurred before running portions of code. We use if statements in order to make sure that certain cases are true before running our code. These are vital to many programming languages. An if statement in C# (and many other languages) can be written as follows:

if (statement == true)
{
  // code block to be executed
}

If statements can also use else cases. These else cases can be used to test whether other statements are true,:

if (statement == true)
{
  // code block to be executed
} else (statement2 == true)
{
  // code block to be executed
}

Or they can be used to just test the opposite of the initial condition:

if (statement == true)
{
  // code block to be executed
} else {
  // code block to be executed
}

What Are Loops?

Loops are ways of making our code repeat itself. If we have a process made up of many lines of code that need to be repeated numerous times, either infinitely or to a certain number of times, we can use a loop. There are many types of loops but the most common are for loops and while loops.

  • For Loops: For loops run a specified number of iterations. In C# a for loop can be written as follows:
for (statement 1; statement 2; statement 3) 
{
  // code block to be executed
}

//Example:

for (i = 0; i >= 10; i++)
{
  // code block to be executed
}

The first loop is the format of what goes into the parentheses. The second loop represents an example of how that format is used. The loop creates a value of i, and as long as that i value is less than or equal to 10, the loop adds one to i and repeats the code in the loop.

  • While Loops: A while loop runs while a statement is true. In C# a while loop can be written as follows:
while (statement == true)
{
  // code block to be executed
}

This code block will continue to run until statement is not equal to true.

What Are Functions?

A function is user defined program that will run when called upon. For example, lets say you have a dozen or so lines of code that need to run every time a player takes damage in your game, this may look something like this if you’re writing in C#:

player.setHealth = player.getHealth - 10;
player.moveBackward(10);
player.stun(5);

So all these lines of code need to run when a player takes damage, but instead of rewriting this each time, we may instead create a function that we can call every time the player needs to take damage, like this:

static void takeDamage(){
     player.setHealth = player.getHealth - 10;
     player.moveBackward(10);
     player.stun(5);
}

The “takeDamage” represents the name of the method. Later we will need this to call the function like this:

if(player.isHit){
     takeDamage();
}

We can call our function in just one line, rather than having to call all three lines every time. This helps with abstraction and making your code a lot shorter and simpler. You can also use arguments which are incredibly helpful as well. Lets say that the value “5” being passed into the player.stun() method represented the amount of time the player was stunned. If we wanted this to change based on the type of hit, we may want to have a variable here that could be changed by an argument for the function.

static void takeDamage(int stunTime){
     player.setHealth = player.getHealth - 10;
     player.moveBackward(10);
     player.stun(stunTime);
}

Now we need to referenece our argument when we call our function, passing an integer variable into our function.

if(player.isHit){
     takeDamage(10);
}

What Are Methods?

Methods are functions that are tied to certain data types. A method can be called by a variable or a straight data type value, this allows for methods to run preset functions written by the authors of your programming language. Methods allow for strong manipulation of variable types which can be incredibly helpful. For example, if you’re manipulating a string and need to make sure that all of your players in your game have lower case names, you may want to try something like this:

string playerUserName;
playerUserName.ToLower();

Methods can also use parameters or arguments which will help further specify your manipulation of your variables.

What Are Arrays?

An array is a list of items. These can be made up of strings, integers, or even booleans. These are a pretty complicated data type and I will have a section written on this later in the post, but simply put, this is a way of accessing numerous data entries with only one data type. . Each item in an array is given an index. When called, this index represents the value in the array that it correlates to. To create a list of strings in C# one can write:

string[] characters = {"Archer", "Barbarian", "Giant", "Wizard"};

A string of numbers could be created as such:

int[] damageValues = {"10", "15", "20", "100"};

Now, if we were to want to use a value in the array of characters we could use:

characters[1];

Now, looking at this, what value would you first assume this would call? “Archer”? You would be wrong, lol. Arrays start at index 0 in most programming languages therefore this would return “Barbarian”. Arrays can have values added, subtracted and modified from them making them very useful for managing multiple values or game objects in a scene. In C#, for each loops are the best way to rifle through arrays and perform code for each item in an array.

foreach (int i in damageValues){
     //code to be executed
}

This loop, will create a variable i, which will represent the value of damageValues[1] and will riffle through each index of the array and perform a task per item. This allows users to use conditions, (ex. if i > 15), or choose to print or log something to the console for each item in the array.

Finally, What Are Comments?

Comments are most programmers downfalls. Being the introverted loners most of us turn out to be, its hard for us to clearly explain what our programs do. Thus, comments were developed. Represented by // they allow us to write text into our programs ignored by the computer. We use comments to describe what our code is doing at that moment or to explain the purpose of methods and functions so that someone reading our code later can easily understand what everything does and how it all works together.

Leave a Reply

Your email address will not be published. Required fields are marked *