Day 5: Variables: The Building Blocks of Programming.
Objective: To give a brief overview explaining the importance of Variables.
So you’re continuing your journey into programming and you are starting to get better at writing code. But you realized every time you want to make a change you have to go to every place you put a number or string at. Oh but wait you forgot to change in on this one line. Or you misspelled something. Seems like you ran into a problem. This is where Variables come in.
Variables come in multiple “flavors”. And I’m going to try to cover as much as possible while making it easy to understand.
Variable (programing definition) : Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves.
A Variable can also be looked at as a box that can hold data. A Variable Name can be looked at as the Label for that box.
Variables also come in different types (for strongly typed languages such as Java, C++, C#, C and some others I can’t remember right now).
Some of those types Include:
Int: Integer value (whole numbers): 2,3, 500, 1000000;
Float: Floating point value (decimal number): 2.1, 4.5, 100.99.
String: A sequence of characters surrounded by “Quotation Marks” ← String
Bool: True or false.
To define a Variable in C# (programming language used in Unity) we define them like this:
int wholeNumber; //this will initialize it to 0.
int wholeNumber = 3; //this will initialize it to 3.
float floatingNumber; //this will initialize it to 0;
float floatingNumber = 3.5f; //this will initialize it to 3.5. If the “f” is not at the end the compiler will complain.
string stringWord; //this will initialize it to and empty string or null
string stringWord = “Hi”; this will initialize it to Hi
bool trueFalse; //this will initialize it to false. By default;
bool trueFalse = false; //this will initialize it to false by assignment.
bool trueFalse = true; //this will initialize it to true by assignment.
Hope this helps. Happy Dev’ing.