### Variables

In math, we use variables to solve equations. For example, when we say x = 2, we know that x is a variable and it equals the value ‘2’. Since we know that x = 2, we can say that x + x = 4. Variables are useful because they give insight to a problem. For example, let’s say I want to calculate the volume of a sphere. We can do the following calculation:

`$\frac{4\ast 3.1415\ast {3}^{3}}{3}=113.1$`

If we showed just this equation to someone else, and asked them what this equation is for without any context, they might be confused. Let’s replace the numbers with a more meaningful equation:

`$\frac{4\pi {r}^{3}}{3}=\text{volumeofthesphere}$`

Where
`$\pi =3.1415$`

Now we know that if we take two constant numbers 4/3 and pi, and multiply it by a radius, we can get the volume of a sphere. Naming a variable can help a person understand what the problem is doing. Furthermore, since you know that the equation calls for a radius, you can calculate several volumes of different spheres if given different radii.

This is just one example of a variable in a real life application. Another example is a bank account - consider a single bank account number. This particular bank account stores money and can vary its monetary value by withdrawing or depositing into the account.

### Types

There are also variables in code. We can make different types of variables; we will focus on the most basic ones:

`int`

, any integer, such as 1, -5, or 391

`char`

, any character, such as a, f, k

`string`

, any group of character, such as hello, thisIsAlsoAString

When you want to declare a variable, simply state its type and the variable name:

`type nameOfVariable;`

Notice how the name of the variable does not include any spaces, and has a semicolon at the end. These are simple “grammar” rules in coding that we must follow so that the computer can understand what we want it to do. If we want to assign a value to that variable, simply write the following:

`type nameOfVariable = value;`

Here are some more examples of how a variable is used:

```
1
2
3
4
5
```

```
int x = 5;
char theLetterC = 'c';
String hello = "Hello World";
```

Notice how char’s value has ' ' while string’s value has " ". Again, this is just syntax that the code follows.

### Conditionals

Conditionals are extremely useful in order to structure your code better. Below are some of the most basic conditionals found in almost all programming languages.

#### If / Else

Whether it’s deciding whether to wear a sweater vs a raincoat or deciding to spend the weekend at the house or with friends, if-else conditions are relevant in every decision you make. Depending on the type of program you make, you can use if-else conditions to make your code run only if it hits a certain condition. Generally, if-else conditions are written as follows:

```
1
2
3
4
5
```

```
if (statement) {
// Code goes here
} else {
// Code goes here
}
```

Let’s take a look at an example. Let’s say we want to make a slot machine game. A player pulls the handle and receives a three digit number. If the three digits are the same, then the player will see the message, “You won!”. If the player receives three digits that are different, then the player will see the message, “Sorry, try again!”

To code this, we have to recognize that if the player gets three of the same number, then all those variables will have the same value. For example, let’s say the player has three variables, x, y, and z. If the player has x = 5, and the player wins, variables y and z must be assumed to be the same value of x such that x = y = z = 5. If the player has lost, then y and z can be any other number in such a way that x ≠ y ≠ z. Let’s put this thought into code!

```
1
2
3
4
5
```

```
if (numberOne == numberTwo && numberTwo == numberThree) {
Serial.print("You win!");
} else {
Serial.print("Sorry, try again!");
}
```

The
`&&`

sign means “and”. So, if x == y and y == z, then all three values are the same. If the condition is not fulfilled, then the statement will print “Sorry, try again!”. A programmer can put multiple if statements if desired. To do that you'd use a structure like this.

Say we wanted to make the slot machine game reward the play if they receive two out of three same numbers. We'd use an "or" which looks like this
`||`

. Check this out.

```
1
2
3
4
5
6
7
```

```
if (numberOne == numberTwo && numberTwo == numberThree) {
Serial.print("You win!");
} else if (numberOne == numberTwo || numberTwo == numberThree || numberOne == numberThree) {
Serial.print("You half-win!");
} else {
Serial.print("Sorry, try again!");
}
```

Now if two but not three numbers are the same they player wins something. Now we have three separate conditionals at work. We used an else if statement to add that third condition. You can use else if statements to make as many additional conditionals as you need. The code below uses 4.

```
1
2
3
4
5
6
7
8
9
```

```
if (condition) {
// Code goes here
} else if (condition) {
// Code goes here
} else if (condition) {
// Code goes here
} else {
// Code goes here
}
```