1.1 Variables - NO-skcaj/FRC-Cpp-Crash-Course GitHub Wiki
What is a Variable?
A variable is data that exists somewhere in memory. You access its data by looking at the address of that memory and either reading it, or modifying it.
So what does this look like?
// Initializes a variable of type "int" but does not hold any value, therefore x is a null (or empty) value.
// However, this initialization does reserve some space in memory to hold data in the future. In this case, 4 bytes.
int x;
// using the "=" operator, we can change the value to another value of type int, in this case "3".
x = 3;
Note: Every line ends in a semicolon
Types of variables
All variables are eventually represented as 1s and 0s (in bytes), but different types of variables allow programmers to more easily work with different types of data.
The most familiar might be int, which is just short for integer (a signed(+/-) natural number). Here are some common ones you'll need:
Name | Description | Size (Bytes) |
---|---|---|
int | A non-decimal number that can be negative (aka signed). | 2 |
float | A signed number that can be a decimal (aka floating). | 4 |
double | A signed floating number that's double the size of a float | 8 |
void | An empty data type, normally used for functions that don't return. | 0 |
bool | A value type that can represent a 0 or a 1, aka true or false (Boolean) |
1 |
char | A single ASCII character, used in an array to make words | 1 |
So which one do I use?
Numbers Always, always, always choose the smallest variable possible. For numbers you want to use an int unless you need decimals where you need to use a float, unless you need a greater number of digits, where you should use double.
Boolean When you need to define a condition, so if something is true and you want to hold that state in a value.
Void When you have a function (which we'll get into later) that you don't want to return, you would use this.
Char When you want a word or some kind of identifier that can't be represented by a number. Later on we'll talk about enums, so if you're wanting to represent what kind of thing something is using a char, just keep going!
Operators
Math
There are 5 arithmetic operators, these generally operate on number types like floats, doubles, and ints.
Symbol | Function | Example | Notes |
---|---|---|---|
+ |
Adds two values | int x = 3 + 4; |
x is 7 |
- |
Subtracts one value from another | int x = 5 - 2; |
x is 3 |
/ |
Divides one value from another | float x = 5 / 2; |
division returns a float, x is 2.5 |
* |
Multiplies 2 values | int x = 5 * 2; |
x is 10 |
% |
Modulo: gets remainder of division | int x = 4 % 2; |
only non-floating types, x is 0 |
++ |
Increments a value by 1 | int x = x++; |
it just adds one. x is x + 1 |
-- |
Decrements a value by 1 | int x = x--; |
it just subtracts one. x is x - 1 |
Use of an arithmetic operator represents an expression, meaning it returns a value. i.e. the expression 3 + 4
is the same as7
. Therefore, we can say that x = (3 + 4);
and x = 7;
are equivalent.
Note: we used parenthesis () to encapsulate the expression you can use parenthesis like you would in a calculator to organize an expression.
Boolean logic
There are 3 boolean operators which describe a comparison between 2 boolean values.
Symbol | Function | Example | Notes |
---|---|---|---|
! |
NOT/Negation | bool x = !true |
"Flips" a bool, x is true. |
&& |
AND | bool x = true && false |
If both values are true, returns true. x is false . |
|| |
OR | bool x = true || false |
If at least one value is true, returns true. x is true . |
Same with arithmetic operators, using a logical operator represents an expression that you can assign to a variable. true || false
is the same as true
, so x = true
is the same as x = true || false
Comparison
There are 6 Comparison operators that all take 2 values and return a boolean value (true or false) based on a comparison of the relation of the 2 values.
Symbol | Function | Example | Notes |
---|---|---|---|
< |
Is less than | bool x = 7 < 4; |
x is false |
<= |
Is less than or equal to | bool x = 4 <= 7; |
x is true |
> |
Is greater than | bool x = 7 > 4; |
x is true |
>= |
Is greater than or equal to | bool x = 4 >= 7; |
x is false |
== |
Is equal to | bool x = 7 == 4; |
x is false |
!= |
Is not equal to | bool x = 4 != 7; |
x is true |
Fairly self explanatory, and you can extrapolate how to use this based on the other operators. So, we're going to look at more complicated examples.
Bringing most of it together
Using multiple of the previous examples you can create advanced, complicated expressions, e.g:
bool x = (!(7 == 4) && (100 >= 5)) == (!false != (8 >= 8));
So what is x
? a lot so lets go through this one step at a time.
Left side: These parenthesis encapsulate each expression just like in math so we can work inside out. The left side of the expression looks like this: (!(7 == 4) && (100 >= 5))
. We can simplify this to (!false && true)
then (true && true)
so the left side == true
Right side: It looks like this now: (!false != (8 >= 8))
. Again, working inside out: (true != true)
. This statement then equates to false
because true doesn't not equal true. Right side == false.
Now together: The left side is true
, and the right side is false
, making the whole expression equates to
bool x = true == false;`. That finally equates to `false`, so `x` is `false`.
That was a lot, so how can we simplify it? An easy solution is to just break apart the expression in 2 so define a right side and left side, then make the final comparison using the previous definitions.
bool condition1 = (!(7 == 4) && (100 >= 5)); // This is the left side
bool condition2 = (!false != (8 >= 8)); // This represents the right side
bool x = condition1 == condition2; // full comparison
Assignment
Our final set of operators have been hiding within our code throughout this course. The rest of the operators were expressions, they read data and returned a value. Using an assignment operator writes to a variable and changes it to become a completely different value.
Symbol | Function | Example | Notes |
---|---|---|---|
= |
assigns a value to a variable | x = 3 |
x was something else, now it's 3 |
+= |
shortcut for x = x + y |
x += 3 |
int x = x + 3 |
*= |
shortcut for x = x * y |
x *= 5 |
int x = x * 5 |
-= |
shortcut for x = x - y |
x -= 1 |
int x = x - 1 |
/= |
shortcut for x = x / y |
x /= 2 |
float x = x / 2 remember, division requires a floating value |