10.3.1 What is a Variable in Program Code?
10.3.1What is a Variable in Program Code? Computer programs often need to remember data to use later. For example, at a shoe store, the number of shoes always changes. When customers buy shoes, the number goes down, and when new shipments arrive, the number goes up. If we call a variable "Shoes," we can assign it a number, and this number can change based on what happens.Avariableacts like a container that holds numbers, words, phrases, and other values. As the name suggests, variables can change the values they hold. We call both variables and constants identifiers, which are unique names given to something in the program. This is important for how programs run.In our simple shoe store example, we store the number of shoes in a variable called "Shoes." The pseudocode might show that Shoes = 7. Using the equals sign, the program sets the value of "Shoes" to be the number of shoes at the start of the day, which is 7 in this case. Later in the code, the programmer types the name of the variable "Shoes" to get its value.Variables can also store Boolean values. If a user logs into an account, the program tracks whether the user is signed in. When the user logs in, this variable stores the value true. When they log out, it changes to false. Constants Sometimes, a programmer wants to keep track of a value that never changes. For example, a physicist might want to keep track of the value of pi to fifteen decimal places. Because this value never changes, they use a constant. A constant is like a container that holds values, but unlike a variable, constants never change—once they're set, they're set for good. With this constant, the physicist just types the name of the constant instead of the whole long number.