Concept of Variables for kids?
Where do you store your things? Salt and Sugar in containers? Toys in a box? or wherever you would like to. Storing is a common task that we all do in different ways. I am sure you must have thought of many other examples. In the programming language, Variable is our container that we use to store different types of information. The best way to understand any programming concept is to imagine its functionality in our daily life. Let us understand the variables with an example:
Monu is a 9-year kid. He always checks his height on his birthday and records it in his diary. Every year when his height increases he cuts the last value and writes the new one. So, What is the variable here?
I hope you can guess that the diary is variable here. Why? because we are storing information in it and we can vary(change) the information we store in it.
What will we learn?
- What is a Variable?
- What role do name and value play in a variable?
- Why are Variables important?
- How to create a Variable?
- Different types of variables?
What is a Variable?
In simple language, Variable acts as a Container to store a value that can be manipulated(changed). The data stored in variables can be used throughout your program.
What do we mean by manipulation of the value?
Let us understand this with an example. You can even try this with anyone: let's say you are thirsty, so you asked for a glass from your mother. This is a clear glass, and you can see that it is empty. It is simply the storage container for the liquid you choose to put in it. In this example, your cup acts just like a variable, and the liquid you put into it is a value.
The cup looks different when it is holding coffee vs. juice vs. water, but it is just the value inside that is changing. Similarly in a variable, we can keep the same name and change its value whenever we want.
Here, Glass = Variable name
and Orange Juice, water, coffee are different values for this variable
What role do name and value play in a variable?
Variables have a name and value. Name is given for the identification of the particular variable as in a program we can have multiple variables. The information stored in the variable is called value. value can be in the wide range including Numbers, Characters, etc.
We must give a meaningful name to the variable which justifies the information we are storing in it. Let us understand the significance of a good name with an example:
Imagine you are going to shift to a new home. You packed all your belongings in the boxes like all toys in one box, Books in another box, but irrespective of what that box has inside you named them randomly. Now it's your class test and you want your books box but many boxes are lying there. Which one is your book box? You picked up the box that has books slip on it but when you open it you get toys inside. Now can you imagine how much short our work would have been if your box name was meaningful to things it has inside?
Similarly, when we work with programming languages the name we assign to our variable should be accurately descriptive(which describes our container perfectly) and understandable to another reader. Sometimes we are that other reader when we revisit a program that we wrote months or even years earlier. Else, we will get confused, and debugging(finding error) becomes tough for us and remember one thing when you are a programmer you have to make it convenient for everyone.
Why are Variables important?
Till now we have learned what are Variables? How they should be named? What is the value? But Why are we studying Variable? What is the need?
If someone asks you to bring water, you will bring a glass of water. Why is the glass required when he needs only water?
let us understand the importance of Variables:
- For Writing Flexible Programs - Variables plays a significant role in computer programming because they help us to write flexible programs. It means rather than entering the data directly into a program, a programmer can use variables to represent the data. When we run our program, the variables are replaced with the value stored in it.
Let us understand this with an example-
In a class of 30 students. The teacher has to prepare a report having names of students below the particular weight(let it be 30kg). let us imagine students are in 7th class and we wrote a program for this calculation and we created a variable and named it to weight instead of checking everyone's weight with 30kg we assigned our variable a value of 30. After one year, the same process needs to be repeated now the particular weight was changed from 30kg to 35kg. But as we have used variables we have to change the value of our variable rather than revising our whole program with new values.
How to create a Variable?
There are different methods and predefined structures for creating a variable but for your better understanding let us discuss three main steps that are involved in almost all programming languages.
- Selecting a Meaningful Name for variable - This name must be unique in your program.
- Declaring the variable - This step varies for different programming languages as syntax varies.
- Assign a value to the variable - In this step, We assign the value or we can say information we have to store in the variable.
Different types of variables?
We will be discussing some general variables that exist in almost every programming language.
A string is a type of value that can be stored in a variable. A string is made up of characters(in a simple way understand it as a single alphabet) and can include letters(a,b,c), words(cat, bat), or symbols(We can also call them special characters, check them on your phone's keypad). We can also say a string is a collection of characters.
Think about the way you remember the names of the people around you. A name is just like a string: it is a value made up of characters (letters) that your brain stores and uses to identify another person. Similarly, you can use a string in programming to label and store values in your code.
Strings make communication easy for us. Now try to find some strings near you. You must have got many things in your mind like your name, and the best example is the line you are reading this is nothing but a bunch of strings.
In programming, an integer is a numeric value typically stored in a variable, so it can be used in mathematical expressions.
Imagine you are the football captain of your team, and you want to add up the number of goals your team scores in each game in a particular season. First, you would store the number of goals scored in each game of that season as integer variables. Next, You will add up all the variables together to know the total goals in the season.
A Boolean contains the value true or false. They answer yes-or-no questions. They are used in various programs for better functioning.
Let us play a game together. You have to answer some questions and you have to follow the only rule that is you have to answer by saying True(if you think it's correct) or False( If you think it's wrong).
Are you ready? Here T= True and F= False
1. Plants give us oxygen. (T/F)
2. There are eight days in a week. (T/F)
3. Camels are known as the 'Ship of the desert'. (T/F)
4. The Sun rises in the east direction. (T/F)
5. Caterpillars turn into butterflies. (T/F)
It's okay if you don't know some answers at least you tried. The main motive behind this small Quiz was to tell you that the questions that can be answered using True/False are Boolean Expressions.
Answers for the above question are 1->T, 2-> F,3->T,4->T,5->T.
So far, we have learned different variables like Strings are a group of characters, like hi or hello, and Integers are numbers, like 5, 50, and 500. We have also learned that a variable can hold only one value, for illustration: String_Variable ="name" or Integer_variable=3. But what if we want to store multiple values? The solution is an Array.
Let us not get deep into this just remember if we want to store integer value, String value, etc in a single variable Array is used for that purpose.
Do you know in many programming languages we call Integer, string, and array a data type? In simple words by data type we mean they tell us what type of data we are using like numbers(1,2) are integer data type.
Hence, We concluded that variables play an important role in making our program flexible. Variable has name and value. We should choose a meaningful name of the variable for better understanding. Using variables enables us in writing flexible programs. There are different types of the variable we have are strings, Integer, array, and boolean. There are many other complex data types we have but for basic understanding, these are enough.
Remember to relate everything around you with the variable for better understanding.
Keep following CuriousJr for more updates.