Variables and Operators.pptxStores data for a calculated value in your program. The value it holds may vary or change depending on the conditions or instructions specified in the program.
KameshvraDelaCruz
10 views
12 slides
Mar 03, 2025
Slide 1 of 12
1
2
3
4
5
6
7
8
9
10
11
12
About This Presentation
You may use a descriptive name, making it easier for the programmer to identify it rather than using its actual value.
Naming a variable is very important since it will be used as reference or label when needed in the program.
It is highly recommended that the variable name must be descriptive enoug...
You may use a descriptive name, making it easier for the programmer to identify it rather than using its actual value.
Naming a variable is very important since it will be used as reference or label when needed in the program.
It is highly recommended that the variable name must be descriptive enough for you to understand and remember whenever you edit and access your program.
The name should be precise yet descriptive to identify data it will hold.
Size: 278.26 KB
Language: en
Added: Mar 03, 2025
Slides: 12 pages
Slide Content
Variables and Operators ORIOLEOSTRICHOWLSY2425
Variable Stores data for a calculated value in your program. The value it holds may vary or change depending on the conditions or instructions specified in the program. Requires data type to identify the type of data it will store and a variable name, which will be used as a label to identify the variable or reference when you need to get its value. Syntax Datatype variableName e.g., 1 int redLED1 = 2;
Advantages of Using Variables You may use a descriptive name, making it easier for the programmer to identify it rather than using its actual value. Naming a variable is very important since it will be used as reference or label when needed in the program. It is highly recommended that the variable name must be descriptive enough for you to understand and remember whenever you edit and access your program. The name should be precise yet descriptive to identify data it will hold. e.g., redLED , blueLED , greenLED ….
Rules in Naming Variables The first character of a variable name must be a letter either uppercase and lowercase followed by a combination of any letters, numbers of an underscore symbol (only acceptable special character). A variable name can be of any length, though the compiler only checks the first 31 characters of a variable
Advantages of Using Variables You declare a variable once, but it can be used several times in your program. You may refer to your variable by its name within your program whenever you need it. e.g., pinMode ( redLED , OUTPUT); redLED variable is referring to the red LED that is assigned in pin 2. Instead of writing it as pinMode (2, OUTPUT)
Advantages of Using Variables If you to change its value, you just have to change it once at the declaration section. e.g., you decide to assign redLED to pin 3
Declaring Variables A variable must be declared first before using in the program. This is referred to as variable declaration, which requires its data type and name. If available, the declaration must also have the variable’s initial value.
Initializing Variables Assigning a starting value to a variable during declaration or within the program. For purposes of good programming practice, you need to ensure that a variable has a valid data in it. As assignment operator (=) will be used to assign a specific value to be stored in that variable.
Variable Scope Refers to the visibility or accessibility of the variable within the program. The part where you declare the variable may identify the scope of the variable. The variable is declared on top of the setup() function is because the variables are intended to be accessed or visible with other functions within the program acting as a global variable whereas a local variable is declared inside a specific function and can only be accessed or visible within that specific function only.
Constant Variables Variables with a fixed value that do not change throughout the program. If the variable will represent where a device is connected const int sensorPin = 2; This creates a constant integer type of variable named as sensorPin with 2 as the assigned value. An error will be encountered if you try to change the value of this variable in any part of the sketch.
Data Type Data Type Description Arduino RAM Usage boolean Holds a true or false value 1 byte char Stores a character value 1 byte int Primary data type for whole number storage commonly in 16 bits(2 bytes) 4 byte float Stores number with floating or decimal point. 4 byte int inputVariable1 = 7; This creates an integer type of variable named inputVariable1 with 2 as the assigned value float pi = 3.1416; This creates a float type of variable named pi with 3.1416 as the assigned initial value.
Multiple Variable Declaration Variables with the same data type can be declared with a single line to save space on your final output file. Each variable declaration must be separated with a comma (,).