Constants and Variables

Like this? Share with your STEAM friends...

WHAT ARE CONSTANTS AND VARIABLES?

• A variable is something whose value can vary. In programming, we use variables to store data.
• A constant is something whose value doesn’t change. In programming, we use constants to store information that we know is not going to change .eg. the value of π (pi).

SOUNDS CONFUSING….

It really is not! Let’s start with understanding constants. Before we start, I tend to make all my constants UPPERCASE and all my variables lowercase since that’s how I was taught coding. While you don’t have to follow this practice, it’s usually good to define practices like this for your code and have standard practices, working by yourself or with a team.

CONSTANTS

Examples of constants

• As we said above, the value of π to the decimal place of your choice (3.1415926535) is a constant and doesn’t change.
• The number of hours in a day (24) is a constant and doesn’t vary.
• The Texas state sales and use tax rate (6.25%) is constant for a length of time.
• The number of players allowed to play a game at one time.
• The name of a character in a game.
• Boiling point of water (100° C).

How do we use constants

Constants  consist of two things: a name (LEAD_CHARACTER) and a value (“COOKIE MONSTER”). Depending on the programming language you use, you will also use a reserved word to tell your compiler that you are using a constant. For example, C and C++ use the reserved word const.

Also, depending on the  programming language you use, you probably will have to mention what data type your constant is –

• integer for whole numbers,
• floating point for a decimal number,
• character for a single character like \$ or €,
• string for a phrase or sentence like ‘Hello’ or ‘Please enter your full name’.
• Boolean for true or false.

Let’s look at some code examples of constants:

```const float PI = 3.1415926535 const int HOURS_IN_A_DAY = 24 const float TEXAS_SALES_TAX = 0.0625 const int MAX_PLAYERS = 3 const string LEAD_CHARACTER = "Cookie Monster" const int BOILING_PT_WATER = 100 const char CURRENCY_TYPE = "\$"```

Why do we need constants?

Constants are useful because we can set it once and reuse it throughout the program as needed. Since it’s not set in multiple places, we reduce the risk of errors in our code and it makes debugging and maintenance of code easier.

VARIABLES

Examples of variables

• Your current score in an online quiz (86 out of 100).
• The current time in your time zone (3:56 PM in Austin, Texas).
• Your sales tax on the items in your shopping cart.
• The number of players playing the game at this time (11 players are playing together).
• The current temperature in your place of residence
• Your details to be create an user account (name, address, city, country, zip code, phone number)

Variables are defined in a similar way to constants, there is a reserved word (usually var), data type, name and sometimes it can  be initialized with a value. The main difference is that a constant can not change when assigned it a value, a variable often changes throughout the program based on input or action.

Let’s look at some code examples of variables (some are current values in code, some are to be calculated by formulae:

```var int current_score = 86 var float total_tax = total_cost * 0.0625 var int num_players = 11 ```

```var string user_firstname = "Cookie" var string user_lastname = "Monster" var string user_address = "Sesame Street" var string user_details = user_firstname + " " + user_lastname + ", " + user_address ```

This is how user_details displays:
Cookie Monster, Sesame Street

Like this? Share with your STEAM friends...