Wednesday, June 22, 2016

Basics of Programming Part 3: Operators

not related, just funny, and something I wish was real


Part 1: Variables  |  Part 2: Functions | Part 4: Loops

An OPERATOR is a character or symbol that performs a predefined function. To continue our long tradition of jars and honey, an operator could be taking out some of the honey or adding more in (arithmetic). Another operator could be looking at two jars and see if they are both honey (conditionals). Here is a list of all the operators I can think of at this moment:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (remainder)
= (assign value)
== (true comparison)
! (relational false)
&& (conditional and)
|| (conditional or)
> (greater than)
>= (greater than/equal to)
< (less than)
<= (less than/equal to)

These are not all the operators, but they are very common. The arithmetic operators work how you’d expect. We’ve already covered the difference between = and ==, but for clarification remember that a single = means we’re assigning a value, while == means we’re checking if the relation between two variables is true.

! is a new one, and not one covered in most basic Maths classes (I think; it’s been a long time). This is called a NOT operator. It’s a comparison as well, so != means we’re checking to see if the relation between two variables IS NOT TRUE (false). This can be confusing, or at least it was for me when I first started. So let’s look at a word problem.

I have a bunch of jars of honey, some of which are expired (I don’t know if honey expires, but for this example it does). If I want to get rid of all of the expired jars I need a way to check each jar. So I write a condition to get rid of all of the unfresh (real word :P) jars.

                if(jar != fresh)
     {
           trashJar();//remember functions are actions
     }

This says that if the jar IS NOT fresh (literally fresh is false) then we get rid of it. You could do a different check, something like this:

                if(jar == fresh)
     {
           saveJar();
     }

This will accomplish the same task and for something so simple this would work. But let’s say you have a few different jars that are full of honey, others tomatoes, and others moonshine. If you wanted to toss out both tomatoes (because they’re gross) and moonshine (I don’t know why you would), you could write something like this:

if(jar == tomatoes || jar == moonshine)
{
     trashJar();
}

This is still not a lot of writing, but imagine you not only had tomatoes and moonshine, but carrots, pickles, pig’s feet, zombie viruses, etc. Instead of writing all of this…

if(jar == tomatoes || jar == moonshine || jar == carrots || jar == pickles || jar == pigsFeet || jar == zombieViruses)
{
     trashJar();
}

…you could simply write:

if(jar != honey)//if jar IS NOT honey…
{
     trashJar();…then we get rid of it
}

I think that’s a lot easier. Don’t you? Instead of listing everything not honey you can simply say if it isn’t honey, trash it.

Anyway, in the above example we used the operator || which means OR. Some languages accept both || or or as valid, but we’ll stick with ||. Likewise the operator && means AND. Using all of these operators we can create some very powerful functions, and we can combine them all together if we really wanted to. For instance, if we wanted to throw out jars of honey that were almost empty or expired we could do the following:

if(jar == honey && (honeyAmount <= 1 || !fresh))
{
     trashJar();
}

This conditional states that if the jar is a honey jar AND either the honey is less than 1 OR is not fresh, then we toss it. Parentheses work like they did in grade school. You first evaluate what’s inside, then you can do the rest of the problem. In this case, even if honeyAmount is greater than 1 but it’s expired, then we’ll toss it out (parentheses), as long as it’s a honey jar (first part).

Operators can get tricky, but this first pass should be enough to get you intrigued. Join us next time when we discuss loops. Sounds sexy, right?

Friday, June 17, 2016

Basics of Programming Part 2: Functions()

how I feel all the time
Part 1: Variables  |  Part 3: Operators | Part 4: Loops

Functions are super awesome as they are what make everything happen. The simplest way to describe it is that a function is an action.

Continuing on with the jar and honey analogy from Part 1, let’s say you have your jar. It’s filled with honey, so that’s good. But having the jar and the information that it’s full of honey doesn’t really accomplish much. In order to interact with that jar, and indeed for the jar to be useful, you must perform actions, or functions. Here’s a function:

function openJar()
{
     //code that lets us open the jar
}

*sidebar:  The two forward slashes make what follows a comment, meaning that it will not be read by the compiler; it is purely for the developer to see. Comments are fantastic and you need to use them to describe your code, because when you come back to your code after a month you will have no idea what you wrote or why you wrote it. So comment often!*

So this function doesn’t actually do anything because no code is written inside of it. In order for a function to…function, we need to have information that it can use. Here’s a semi-working function:

bool closed = true;

void openJar()
{
           if(closed == true)
           {
                //we open the jar by switching the value to false
                closed = false;
           }
           else
           {
                //jar is already open; no need to open it again
           }
}

Okay, let’s walk this through bit by bit:

bool closed = true;

This should look familiar from Part 1, but with a different DATA TYPE. Remember that bool or boolean is a true or false value, which numerically is 1 or 0. closed is the name of the boolean variable. true is the value of our closed boolean variable.

Okay that was simple. Let’s see what we can make of the next bit:

void openJar()

Funnily enough, this has the same basic structure as we discussed with variables. void is the return value of the data type. A return value is a value that, when the function is run, is returned to the program. You can actually call a function and put that return value into a variable, though that might not make sense yet. We haven’t talked about void before, but basically it means that when this function is called it doesn’t return any sort of value, it just runs through the function and that’s it.

openJar is the name of the function, just like closed is the name of our variable. It’s just a way to easily identify the function. Simple.

() is a new sign for us. () in this context is known as a CALL NOTATION. I don’t really know what this does in the background, but it’s what lets the compiler know a function is being called. Sorry I can’t go into more detail. I don’t know enough about the inner workings, only how to use it. Just like I can watch TV but I couldn’t explain exactly how it works.

Next up is {}. These brackets are the beginning and end of a function or statement. All brackets need to be in pairs. This can get confusing when there are nested statements (meaning sets of brackets within sets of brackets within sets of brackets, etc), so always create a set of brackets, never just one. Anyhoo, everything within these first brackets are the openJar function.

Next up is, again, something new:

if(closed == true)
{
     closed = false;
}
else
{

}

This is called an “if-else statement”. Basically, it asks a question about the value of a variable. Then, based upon the answer, it will output one of a number of actions. While there are () in this line, they do not have the same purpose as the call notation. Here they are grouping together the two sides of the question being asked. In this case we’re asking if (closed equals true), then do whatever is in the following brackets. if (closed does not equal true) (this is the else part) then do whatever is in the next set of brackets.

The two equal signs (==) act as a comparison, or a conditional, asking whether or not two things are the same. In variables one equal sign (=) means we are assigning a value, saying that closed is true. So the if statement is looking to see if closed equals the same value as true. It checks the value of the boolean variable closed, which we declared and assigned in the beginning, with the value true. Because the value of closed is true, the program runs through and sets closed to false, which means that our jar is now open. The second half, the else part of the statement, will not be looked through, because the condition in the if part of the statement was found to be true.

However, if we were to run the function a second time in a row it would jump to the else part of the statement. Can you think of why this is?

Did you think about it, or are just skipping ahead to find the answer?

The answer is because we’ve changed the value of closed, meaning the if part of the statement will not be true, so it moves on to the next part (else).

This is a very basic function, but it’s a lot to process, so we’ll leave it there for now. If you think of functions like a word problem, you should have an easier time. I tend to write out on paper in English what I want my functions to do, then I put that into the syntax of the program. Sometimes it works how I envisioned, but sometimes I have to rework it. That’s half the fun!

I’m not sure what we’ll cover next time, but I hope you stay tuned for the next installment of Basics of Programming. 

Part 1: Variables  |  Part 3: Operators | Part 4: Loops

Wednesday, June 15, 2016

Basics of Programming: Blog Intro && Variables

this is programming, get used to it
Part 2: Functions  |  Part 3: Operators Part 4: Loops

Programming is super scary for a beginner. It's gibberish to those who don't really know what to expect. This is why there are tons of game engines on the market that boast about not needing to know how to code. I felt the same way, and when I see new code now I still get scared.

But it's a mental thing, because programming is actually quite simple. Not easy, necessarily, but simple. There are basic rules to follow that are, more or less, across the board.


Now, let me get this out in the open: I am not a master programmer. I am a hobbyist programmer for games, meaning I do it for fun, not for skill or work. I am entirely self-taught. I don't know a lot of terms, and I will undoubtedly misuse them. As well, there are probably more efficient ways to do what I will show you, but I'm always learning and willing to learn, so if you come across something I did wrong or inefficient, let me know. 

For now I'm going to cover the basics of what things are in programming. There might not be a lot of code this time around as I just want to cover the basics of code construction. I will also try to keep my code writing consistent, but I've bounced around a lot of different languages so it might differ from time to time. Sorry about that. 

The most basic thing to understand in programming is a VARIABLE. A variable is simply a jar. This jar can be labeled or not. It can be full or empty. It can be stacked in crates or it could be a single jar. Whatever is in the jar is information. This information can be a number, a letter, word, a sentence, and can even be another variable. 

To use or make a variable you have to DECLARE it. I had to look this up to make sure I was using the right word. Here is a basic declaration of a variable:

var jar; 

That's it. This is a declared variable. As of now it is an empty jar. It has no label or information filling it yet but it has been declared. Declaration means it can now be used. So let's use it:

jar = "honey"; 

Now our variable jar is full of the information "honey". The information "honey" is text, which is called a STRING.  

You'll notice I didn't put var before jar this time around. The reason for this is simple: jar has already been declared. A variable only needs to be declared once for it to be used, so we don't want to declare it again. In fact, most compilers will catch this but it's good to know in case you get the error.

You might be wondering why we don't just say "honey". There are a few reasons, but one important reason is that, generally speaking, we want to be able to change the value of a variable at any time. Another would be the contents of a variable might be really long and messy and hard to type a thousand times, where jar is three letters. And probably the most important reason is because that’s how it’s done technically.

Let's say we wanted to change the contents of our jar. We could do something like this: 

jar = "tomato";  

So far we've changed jar to mean "honey" and "tomato". But we could also change it to a number if we wanted: 

jar = 42; 

We can do this because we're using a WEAK TYPED language. Some programming languages don't allow this conversion. These intolerant and exclusive languages rely on STRONG TYPED programming. Here is an example of a strong typed variable: 

string jar = "pooh"; 

Notice I wrote string instead of var. I don't have to say var as a string is a type of variable already. This means that our variable jar has to be a string. I could not do the following: 

string jar = 42;

42 is not a string, but an integer. A strong typed variable cannot be anything other than what its type says it is. If someone asks you for your age, you cannot say “nachos” (no matter how delicious) because “nachos” is not a number. The only answer that fits the question is a number, so no other type will be accepted.

Strong typed programming is what I'm used to using, and I think it's the better way to go, because my philosophy of programming is that everything should be intentional (which is probably why I'm not a big fan of procedurally generated games like FTL, The Pit, or Binding of Isaac; topic for another time). Strong typed programming is not a must and is not the best way to do things, just the way I prefer to do it. As well, weak typed variables have their place too, but that’s a little more advanced topic.

Here are the main types of variables (Data Types): 

Boolean – true or false, 1 or 0. Depending on the language it can either be called a boolean or simply bool 

Integer – just like in Maths class, an integer is a whole number, negatives, 0, and positives. I've only ever seen it as int 

Floating Point Number – all numbers, whether an integer or a decimal. There are many different variations, most of which I don't even know the point of. It could be number, float, double, and some others, though number and float are the main ones I've used and seen 

String – like we talked about and used, this is text. I've only ever seen it as string. Strings also use either double-quotes (“”) or single-quotes (‘’). It doesn’t matter which as long as they are grouped together. You cannot use do this “love’.

Character – similar to a string, but it's just one character (shocker!). I've only seen it as char 

Variables are the basic building blocks of programming (probably not an accurate statement; don't listen to me). Variables are used everywhere and if you understand that variables are just a jar that can be filled and moved from shelf to shelf, then you'll get the rest of programming. Eventually.

I hope you enjoyed this brief intro to programming variables. Up next I'm going to just jump right into functions. I think most people would go to operators first, but I'm a rebel and I'll never be any good. 

To be continued...

Part 2: Functions  |  Part 3: Operators | Part 4: Loops