Home » Variable Examples

Variable Examples

by Online Tutorials Library

Variable Examples

You might have come across the term variable once in your life. Many of you who associated computer programming might be dealing with variables every day. Variables have different meaning and usage criteria for Mathematics and computer programming.

In this tutorial, we will briefly learn the various types of the variable and their specific examples.

I. Variables in Mathematics

A variable is an alphabet or expression that signifies an unidentified number or unidentified value, or unidentified measure. The applications of variables are primarily found in fields of algebraic expression or algebra. For instance, y + 6= 10 is a linear equation where y is a variable, where 6 and 10 are constants.

The variable usually represents a quantity that can be altered or fixed according to the mathematical operation performed. When we talk about variables, you will notice mostly x and y are most commonly used, but this is not specific, and one can use any alphabet. In mathematics, we can describe variables with the help of a function

Parts of Equation

In Mathematics, Variables are present in some form of equations. The different parts that assist a variable in forming an Equation are as follows:

  1. Variable
  2. Constant
  3. Coefficient
  4. Operators
  5. Term

For instance, in equation, 10y+ 19 = 201,

  • y represents the Variable
  • 10 represents the coefficient of y
  • 19 and 21 represents the constants in the above equation
  • ‘+’ represents the operator

Operator: An operator is defined as an arithmetic character that helps to execute several operations on the values. The commonly used operators are + (Sum), – (difference), × (multiplication), /(division), etc.).

Term: A term is a single expression of an equation. It can be a number or a variable or a number multiplied by a variable.

Examples of Variables

The examples of variables are given as follows:

  1. y+20 = 80 + 8x
  2. x-3y = 12
  3. 50x= 10y + 10
  4. 14x * 30 =70

In the above equation, x and y represents the variables.

Constant in Mathematics

In Mathematics, Constants are defined as the values that are fixed and cannot be altered or change further. The concept of Constant is the exact opposite of a Variable. In algebraic equations, the constant is the fixed numbers that define themselves. For instance, in the below equation

10y+20 = 85;

  • Y is a variable (where the values can change), and it has 10 as its coefficient value.
  • The remaining digits, i.e., 20 and 85, are the constants.

Types of variables in Mathematics

In Mathematics, the variables are further can be categorized into two types, that are as follows:

  • Dependent Variable
  • Independent Variable

The detailed description of each variable type is discussed below:

1. Dependent Variables

The dependent variable is defined as the variable whose value varies if there are any changes in another variable’s condition present in the same algebraic equation.

For example, in the below algebraic equation

y = 4x + 3.

You will notice that the value of the ‘y’ variable is dependent on the variations in the value of ‘x’.

  • Therefore, the variable ‘y’ is a dependent variable.
  • The variable ‘x’ is an independent variable.

2. Independent Variables

In a mathematic algebraic equation, a variable is defined as an independent variable if values are independent, if there occur any changes in the equation, or to any other variable present in the same equation. For example: if an equation carries x and y as two variables where the value of y is connected with the value of our second variable x, in that case, ‘x’ value acts as a function variable y value, and thus it is called an independent variable.

For Instance: In the below equation

y = x+7,

  • The variable x is an independent variable
  • The variable y is a dependent variable.

II. Variables in Computers

In a computer programming language, Variables are a standard container used by language editors to hold information that could be further used throughout a computer program. It is also a process through which you can label your data and its type with a specific name so that other programmers can easily understand the programs.

Variables are nothing more than a name you provide to computer memory locations, at which you have stored the information of your program. For instance, you have two integer data values, i.e., 2 and 3, and you frequently want to use these two values within your computer program. To save time and memory, the best option is to store these two values in two variables in computer memory so you can use them anytime. Now, let’s see how you will do it. Here are the following three simple steps –

  1. Create your variables with proper names.
  2. Hold your program values in the created variables.
  3. Reclaim and use the stored values from the variables.

Creating variables

Different programming languages have different formats and syntax for creating a variable. For instance, in a programming language like C, creating a variable is also known as declaring variables. Let’s see how we can create a variable in C:

In the above program, you can see we have created two variables and have named them x and y. These variables are of integer data type as we have specified them using int data type because they want to store integer values in those two variables. Likewise, we can create variables of different data types such as byte, long, float, boolean, char. For example ?

Listed below are the key points about variables that you need to keep in mind –

  • A variable can hold only a single type of data. For instance, if variable x has been declared with a Boolean data type, it can hold only Boolean, i.e., True or False. If the user tries to store any other data type value inside that variable the program, will throw an error.
  • Different languages have different syntax and rules for creating a variable. Unlike in the C programming language, one must declare a variable before using it. In most programming languages, the user cannot use any variable without creating it. In the case of Python programming, you can directly use any variable even if you have not created it.
  • The name that has been used to create one variable cannot be used to create another variable inside the same program. For example, if you have declared a variable x with ‘int’ data type, you cannot declare the variable x again to hold any other data type.
  • In modern languages, unlike0 Python, PHP, Perl, etc., defining the data type while creating your variable is not mandatory. Therefore, you can create a variable that can further store any value such as integer, float, or long.
  • While naming any variable, it is advised to give them the appropriate name based upon what data you want to store inside it, such as age, sex, dob, salary, year2001, or anything else. Different programming languages have their limitation with the number of characters used in their variables names.
  • It is always advised to start the variable names using alphabets only instead of using numbers. Therefore variable 9001years is not considered as valid where the variable name years9001 is a valid one.

Every programming language has its own rules and syntax to define and create its variable. Let’s go further to learn in detail the examples of the store and access the variables. We will also cover the syntax and rules of variable creation commonly used in various programming languages.

Store Values in Variables

Earlier, we have covered the process of creating a variable. Let’s see how we can assign values to those variables –

In the above program, you can see two statements wherein we are storing integer values to our two created variables, i.e., we are storing 40 in x and 50 in y. The process of storing values is approximately the same in all the programming languages where the name of the variables are kept on the left side of the equal sign (=) and the data that you want to store is kept on the right-hand side.

Access stored values in variables

If we don’t access the variables in our programs we have created and later have assigned those values, their existence in the program is a waste. Therefore, we will learn how to access the stored value in variables. In the above programs, we have already created two variables named x and y and have stored two values, 40 and 50, respectively. Next, we will access both the values and print the values stored in these two variables. In the below example, we have written a program in C to print the values of the two variables ?

Code:

Output

Variable x = 40   Variable y = 50   

Example of Variables in Java

You can refer to the program given below for creating any variable in the Java programming language. In this code, we have declared two variables, x, and y, with integer data types. Later we have assigned them integer values of 40 and 50, respectively. At last, we have used the ‘println’ command to print the values of both the two in different ways:

Code:

Output

Variable  x = 40   Variable y = 50   Variable  x = 40 and Variable y = 50   

Example of Variables in Python

You can refer to the program given below for creating any variable in the Python programming language. In this code, we have created two variables, x, and y, and simultaneously have assigned them values of 40 and 50, respectively.

As in the Python programming language, it is not mandatory to specify the data type initially when you are creating a variable.

Code:

Output:

Variable x =  40   Variable y =  50   Variable x =  40  and Variable y =  50  

Example of Variable in C

You can refer to the program given below for creating any variable in the C programming language. In this code, we have created two variables, x and y, and simultaneously have assigned them values of 40 and 50, respectively.

Code:

Output

Variable x = 40 and Variable y = 50   

Next TopicSequelize

You may also like