Lesson 6 - Variables
Variables & Data Types - Overview
Variables are essential for programming in VRC, and it's important you understand all the types and their uses.
First, let’s understand what variables do in general. Variables are used to hold values that you will want to access and/or change later. In other words, variables hold data. Therefore, data types are basically types of variables.
Common Data Types:
Integers (int)
Floats (float)
Doubles (double)
Booleans (bool)
Data Type - Breakdowns
Integers: Integers are any positive or negative numbers without decimals.
Ex: 1, -1, 0, 23, 4000130
Floats: Floats are any positive or negative numbers with decimals. Up to 7 decimal digits of precision.
Ex: 11.5743, 2.00, -39.5, 9.0000001
Doubles: Doubles are the same as floats but have up to 15 decimal digits of precision.
Ex: 11.5743, 2.00, -39.5, 9.000000000000001
NOTE: Unless you need the extra decimal places, use a float instead
Booleans: Booleans can only return 2 values, true or false.
True is represented by a lowercase true or by the number 1.
False is represented by a lowercase false or by the number 0.
All Possible Values: true, 1, false, 0
Declaring Variables (ints, floats, doubles)
There are many ways of declaring (creating) variables. In this demonstration I’ll be declaring an integer called “num” that holds the value 5.
You can also combine those lines into
If I wanted to first declare num as 5 and then change its value to 601 it’d look like this:
After these lines are executed, anywhere I write num in my code is the equivalent of writing 601.
You can also change the value of a variable using arithmetic.
Doubles and floats function almost exactly the same way. A basic rule is you can declare basically any variable using this simple formula:
data_type variable_name = variable_value;
You can see that pattern in
int num = 5;
Declaring Booleans
Booleans have a few special characteristics that are necessary to understand.
Here we declare a boolean called driveToggle that is false.
I can then make it true by doing:
However, I can also do:
This second option turns a true value into false, or a false value into true. In this case, since driveToggle starts as false, it becomes true. If we did it again afterwards, it would go back to false.
Casting Variables
Casting variables is essentially setting the value of a variable of one type with a different type. For example, if I have a float variable called floatVar with a value of 27.3 and an integer called intVar that has no value yet. You can assign the floatVar value to intVar and it will TRUNCATE the float and just take the integer portion of it. intVar will then have the value of 27. The syntax will look like this:
intVar now equals 27
NOTE: If floatVar was 27.9, when casting intVar, intVar will still be 27. The code will truncate, not round.
Variable Naming Conventions
There are three main rules to having clean and readable variable code.
Use consistent conventions. For example, for a variable name with multiple words in it like “Left Chassis Power” you could always keep the first word lowercase and then start all other words capitalized. That would result in “leftChassisPower” as the variable name. You could also just leave them all lowercase and just separate them with underscores like “left_chassis_power”. Whatever you choose, keep it consistent throughout your code.
Make sure your variable names are relevant to their purpose. For example, if you have an integer that sets your chassis power, don’t call it “top”, call it “chassisPower”.
If you’re declaring multiple related variables, declare them all in one line. For example, if you’re declaring two variables “leftLiftPower” and “rightLiftPower”, declare them on one line like:
int leftLiftPower, rightLiftPower;