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

2 comments:

  1. informative and easy to understand. I'll be reading these regularly.

    ReplyDelete
    Replies
    1. Thanks, Kevin! The next one I think will be a bit trickier, but no less fun. :)

      Delete