-
Notifications
You must be signed in to change notification settings - Fork 0
Code
Comments in code are ignored. They perform no action and are there simply as notes you’ve left for yourself or other people. They either begin with a //, or are encapsulated by /* */
E.g.
// this is a comment
/*
This is a series of unfortunate comments
that are progressively getting worse.
/*
The first thing you need to understand is variable types. Each platform has different types, so you'll need to the learn the types of your system. For our case, we are working with an Arduino. The most common variable type is int short for integer. Integers on an Arduino equal 2 bytes, which means the largest number they can represent is 32,767. Other types include:
| Type | Value | Comment |
|---|---|---|
| int | 2 bytes | Can store numbers as large as 32,767 |
| float | 4 bytes | For storing numbers with a decimal component e.g. 6.969 or 4.20 |
| bool | T/F | Handy for storing true / false values ONLY |
| char | 1 byte | Represents an ascii character |
In programming, we like creating human readable words to represent numbers or expressions. These are called variables. We must create variables before we can use them. Variables also have to be of some type, so we when we create a variable, we tell the computer what type they are. They follow this simple format:
(Type) (variableName) = (someValue); i.e. int a = 5; & bool happyBartender = false;
Notice how after each statement we end with a semicolon? We always terminate statements with semicolons. After the variables have been created, we can use them as we wish, we don't have to re-make them.
int flowers = 10;
int result = 0;
result = flowers / 2;
// result now equals 5
We can easily change the value of a variable. In the code above, result was equal to 0, then we quickly set it to equal whatever (flowers / 2) is.
This may seem strange, but we can absolutely use the following code: flowers = flowers / 2;. flowers starts off by equaling 10. This statement says, "flowers new value is going to equal flowers (aka 10) / 2." flowers will equal 5 after this.
In C++, anywhere you see a number value, you can replace it with an expression. For instance with int b = 11;, the 11 can be replaced with the expression, (6+5). In further detail:
int a = 5;
int b = 15;
int c = a + b; // c = 20
int d = (a + b) / 2; // d = 10
// don't confuse comments // with division /.
Homework:
Create 2 variables, giving them values of 20 and 10. Create a 3rd variable that equals the multiplication of the first 2 variables.
Show Answer
int a = 20;
int b = 10;
int c = 20 * 10;
Hello!