-
Notifications
You must be signed in to change notification settings - Fork 1
CPP Beginner's Guide
This guide serves to explain basic syntax. It will not cover more meta-topics (such as compiling and selecting a development environment) and is merely about how to write the code and understand it in the context of basic FRC development.
Variables are the most basic "thing" in programming. A variable can store values of its type. Some types are numerical (int
, float
, double
, long
) and some are not (string
, bool
, char
).
int
, short for integer, stores a whole number. long
can store larger numbers. float
can store decimal places, and double
s are larger floats. char
stores a single character, such as 'a' or 'b', and a string
is a list of char
s arranged to make a word (such as "abc" or "this is a string"). A bool
, short for boolean, is always true or false.
Variables must be both declared and assigned. Declaring a variable is simply saying it exists. This is done by first stating the type (seen above) and then naming it. Names must not start with a number, but can contain them anywhere else. Generally, when naming variables, they should be short but descriptive and conform to a style of your choice. Remember that all statements must end with a ;
.
Here are a few examples:
int someInteger;
float someFloat;
bool someBoolean;
After declaring variables, they must be assigned values on the same line or a new one (depending entirely upon your code style). This is done by using the assignment operator, '='. If declaration and assignment are on the same line, the formula is <type> <name> = <value>;
. If they are on different lines, the assignment is simply <name> = <value>;
.
Here are a few examples:
int someInteger = 0;
float someFloat;
bool someBoolean;
someFloat = 0.67;
someBoolean = true;
Whenever you want to set a variable to a new value, just use that assignment operator.
Because many variables are numerical, there are some math operators that can be used to get new values and compute different things. You can use them with raw numbers or numbers stored as variables.
The most basic are as follows:
+
- Addition
-
- Subtraction
*
- Multiplication
/
- Division
Here is an example of them in use:
int one = 1;
int two = 2;
int three = 3;
int sum = one + two; // sum is assigned to be 3
int difference = three - one; // difference is assigned to be 2
int product = three * two; // product is assigned to be 6
float quotient = three / two; // quotient is assigned to be 1.5 - note how this is a float because of the decimal value
C++ has built in order of operations, so any multiplication or division will be performed before addition or subtraction. You can use parentheses to avoid this.
int ooo = 4 + 2 / 2; // 2 / 2 is calculated first, making this equal to 5.
int other = (4 + 2) / 2; // 4 + 2 is calculated first, making this equal 3.
Sometimes you need to set a value to a permutation of itself. For this you can use the assignment operators +=
, -=
, *=
, and /=
.
Basically, the statement someVar -= 12;
takes whatever value someVar
is equal to, subtracts 12, and assigns the difference to someVar
. Here's an example:
int value = 6;
value *= 6; // value = value * 6, which means value is 6 * 6, which makes it 36.
An even quicker method for incrementing or decrementing a number by 1 is ++
.
int i = 0;
i++; // Now i is equal to 1.
Sometimes, it can be useful to store multiple values together. This section will talk about the two most common data sets: arrays and vectors.
In C++, arrays are fixed-length sets of a single type of data. This means it can have only a specified number of values and each value must be of the same type. There is a special way to create them: <type> <name>[<number>] = {<val1>, <val2>, <val3>, ... <valn>};
Here is an example of an array of doubles:
double someDoubles[5] = {15.89, 3.64, 1.1, 59.78, 2.22};
Sometimes, a fixed-length array doesn't cut it. Whenever you need a variable-length array, you can use a vector. They are a bit more complicated syntactically at first, but with practice you'll become accustomed to them.
The first issue is that it is not a default data type. You have to use what's called an include statement. For more information on them, see the Files section below (to be added).
The statement you want will be at the top: #include <vector>
After this, you can use vector as many times as you want. To create one, use the keyword "vector" and then in angle brackets put the type. This is because it is a template class, meaning its functions change what they do based on the type it is given. More on this later.
#include <vector>
vector<double> someDoubles = {15.89, 3.64, 1.1, 59.78, 2.22};
To add more values to the vector, you cannot use +
. Instead, use the following: <name>.push_back(<value>);
#include <vector>
vector<double> someDoubles = {15.89, 3.64, 1.1, 59.78, 2.22};
someDoubles.push_back(789.123); // Now someDoubles is equal to {15.89, 3.64, 1.1, 59.78, 2.22}
To grab a specific value from a data set, you need to reference it at the proper index. Each value is at a numerical index in the array. Because binary begins counting at 0, all data sets start at index 0. Given any array we can grab the 3rd, 4th, 5th, or nth value from it by surrounding n-1 by square brackets. Here's an example:
double someDoubles = {15.89, 3.64, 1.1, 59.78, 2.22};
someDoubles[0]; // The 0+1 value of someDoubles, 15.89
someDoubles[1]; // The 1+1 value, 3.64
someDoubles[4]; // The 4+1 value, 2.22
Remember, all arrays start at 0.
All programs are running loops that end when the program is closed or paused when waiting for a certain action to occur. There are two main types of loops, for
and while
. A for loop goes until an incremental number reaches a certain value and a while loop occurs until a condition of any kind is no longer true.
To create a for loop, you need a three-statement condition. The statements are the integer to be used and its initial value, the value(s) for which the loop will run, and how to change the integer after each iteration. The integer is typically called i
unless it is nested within another for loop that already uses i
. Here is an example of setting up a for loop:
for (int i = 0; i < 5; i++) {
/*
* Do some stuff here
*/
}
In the above example, we start out with an integer called "i" and assign it to be 0, run the loop until i
is no longer less than 5, and set i
to be i+1
at the end of each iteration. This means it will run 5 times, when i is 0, 1, 2, 3, and 4.
You can also use i
within the loop to represent which loop you're on or anything else involving an incremental value. For example:
#include <vector>
double doubleArray = {15.89, 3.64, 1.1, 59.78, 2.22};
vector<double> doubleVector = {};
for (int i = 0; i < 5; i++) {
doubleVector.push_back(doubleArray[i]);
}
Each iteration, the corresponding value of doubleArray
is added to doubleVector
. After the loop finishes running, the two will be identical in value.