Friday, June 4, 2021

Storage and Variables

The Human’s usually forgot something to do, due to busy schedule or work load.

We may don’t remember to pay bill?

We may forget to mention last-minute tip in our presentation!

We may forget to switch off veranda light.

But with computer, it doesn’t happen. Because they don’t forgets anything, if properly programmed.

Asking the computer to remember

Let us try telling the computer to remember Tom’s name.

OK Computer:

Remember Tom as my_name

Display my_name

 

Output:

     Tom

 

In this oversimplified example, we asking the computer to remember Tom’s name and display it finally.

The computer will remember Tom’s name until we tell it to store a different name.

Variables

To remember things, computers use memory boxes called variables.

Like boxes, variables have a label and store content in them.

The sequence for storing an animal name in a variable [memory box]

OK Computer [main()]:

Create my_pet

Remember “Pug” as my_pet

Display my_pet

 

Output:

     Pug

Here the label of variable is my_pet and its content is Dog.

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     char *my_pet;

     my_pet = “Pug”;   

puts(my_pet);

}

Output:

     Pug 

 

In computer language, labels have to be named with some rules, such as:

  • Labels have to be single letter or single word.
    • I, x, my_pet, Rocky_Handsome etc. are valid labels
  • Labels may contain or start with underscore and not with any other symbols.
    • _animal, height_ etc. are valid labels
    • My_Quest?, /_age etc. are invalid labels
  • Labels may contain numbers, but not start with it.
    • student_1, box27 etc. are valid labels
    • 1969, 10100,1_must etc. are invalid labels

In simple terms, labels named with a-z, A-Z and underscore symbol. They don’t have to be real words, but it’s smart to use meaningful labels.

Changing values of variables

Can variables change values?

Let’s find out:

OK Computer[main()]:

Create my_name

Remember “Rocky Handsome” as my_name

Remember “Tom” as my_name

Display my_name

 

Output:

     Tom

Here my_name is the label of variable, which initially stores Rocky Handsome then Tom. So when display command is called computer prints Tom as my_name.

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     char *my_name;

     my_pet = “Rocky Handsome”;   

puts(my_name);

my_pet = “Tom”;   

puts(my_name);

 

}

Output:

     Rocky Handsome

     Tom 

As we saw here, the variables are capable of changing values as the program runs. So we can use variables to assign values and keep track of things.

When we use storage?

During the programming, programmer needs to store some information for future manipulation. In such times storage is a necessary option.

Usually we need to store different kinds of or different types of information. Most widely used information types are numbers and words.

Using variables

Most of the computers are great in carry out arithmetic tasks.

OK Computer [main()]:

Crete my_number

Remember 10 as my_number

Add 50 to my_number

Subtract 10 from my_number

Multiply my_number by 3

Divide my_number by 5

Display result

 

Output:

     30

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     int my_number;

     my_number = 10;   

my_number = my_number + 50;

my_number = my_number - 10;

my_number = my_number * 3;

my_number = my_number / 5;   

printf("%d", my_number);

 

}           

Output:

     30

    

Note that even if we were mathematicians, it would be hard to beat the speed and accuracy of computers.

Storing text

Unlike numbers, words or strings have (" “) double quotation around them and can't be used for arithmetic operations. They are used to store text like information.

Hey Computer:

Remember my_pet as “Dog”

Add “Cat” to my_pet

Display my_pet

 

Output:

     DogCat  [not Dog Cat]

Here the two structures are combined without space. As spaces are also part of strings we have to add them if we want to see them in the result.

Hey Computer:

Remember my_Greet as “Hello ”

Add “world!” to my_Greet

Display my_Greet

 

Output:

     Hello world!

Lists

As we saw, variables are like memory boxes which store values. What if we want to store multiple values? This is where we use boxes with space to store multiple values: also called as lists.

Hey Computer:

Create a list

Add “Dog” to the list

Add “Pug” to the list

Display list

 

Output:

     Dog

Pug

In c language, if programmer wants to implement above task into his code form then below is the details:

#include <string.h>

main()

{

    char *my_pet[2];

    my_pet[0]="Pug";

    my_pet[1]="Tug";

    puts(my_pet[0]);

    puts(my_pet[1]);

 

}           

Output:

     Pug    

     Tug

Notice that? Lists are suitable to store multiple values, particularly when they are of the same type.

For example list of phone numbers which is numbers list, and list of students name which is text list.

Summary

What are variables?

  • They are memory boxes with labels
  • Computers use them to store and remember stuff
  • They can change their values

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

No comments:

Post a Comment