Digital technologies/Arduino/Arduino- Beginner/Introduction to Variables and Conditional Statements

From CEED Wiki
Jump to navigation Jump to search

Variables

Variables allow  information in programmers to be stored or changed within the code. In order to create a variable within your program, it must be declared. To declare a variable, the coder has to write the type of variable to be declared first. Different types exist, most commonly used are:

Table 4: Summary of commonly used variable types
Type Syntax
Integers int
non-integers (rational and non-rational) double or float
characters char

After declaring the variable type, the coder must then name the variable. The variable name should clearly reflect the purpose of the value, so that the coder and the reader can easily identify which value is associated with what variable. Additionally, variables cannot have spaces embedded within the name. For example, "My deposit" is not a valid variable name, but "MyDeposit" is. Note that capitalization can be useful for readability. Furthermore, variables should not start with digits, or with an underscore "_".

After naming the variable, the coder can choose to initialize the variable, which is choosing an initial value to be associated with the variable (which can change throughout the program). Be careful to stay consistent with the type of the variable. For example, you cannot initialize the variable int Age with a value of "k", as "k" is not an integer. The code segments below illustrate the process of initialization:

int a;// not initialized, which is ok!
double b=1.23;/* Note that the 1.23 value matches with the “double” type which is a rational number*/
char MyVariable = “h”; //note the semicolons!

The following will produce an error, which will disable the compiler from compiling your code:

char YourVariable= 1.23; // 1.23 is not a character!

Variables can be classified into either global or local variables. The main difference is the scope at which they can be accessed. A global variable can be accessed by all functions that exist within a program, whereas a local variable can be accessed through only the function where it is declared. The example below illustrates this well [https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/]:

/* There are two functions in this program: setup() and loop(), and three variables: "k", "i", "f".*/
int k;  // any function will see this variable, thus called a global variable.
void setup() {
  // ... }
void loop() {
  int i;    // "i" is only "visible" inside of "loop"
  float f; }  // "f" is only "visible" inside of "loop"
.....// some lines of code}

Conditionals

Conditional statements are programming operations that tells the computer to perform an action for a certain set of conditions. An "if" statement is the most common, and tells the computer to perform a set of actions only if the condition is fulfilled. The syntax is the following:

if (condition) {
/// a set of instructions }

Common alteration of the if statements are the "if...else" and "if... else if". The "if...else" conditionals first checks the "if" conditional, in the case that its true, the compiler will ignore the "else" set of instructions. In the case that the condition(s) declared in the "if" statement are not fulfilled, the "else" instructions will automatically run. The syntax for an "if...else" statement is:

if (condition){
///instructions;}
else {
///instructions that will ONLY run in the case the "if" conditions are not fulfilled }

In the case of the "if...else if" set of instructions, the program will only run if the conditions specified in the "if" or "else if" are true. The syntax is the following:

if (condition){
///instructions }
else if (condition) {
///instructions }