How to store multiple values
In the previous post we studied how to store values, but how do programmer store the multiple values? The solution is to create a list of values just like Tom’s mother creates grocery list before visiting any general store nearby!
How to prepare a sequence where multiple values are stored in a single variable!
For example, Tom is preparing a vegetables list for shopping:
Hey Computer:
Remember “Cucumber” as Vegetables
Remember “Lady Finger” as Vegetables
Remember “Carrot” as Vegetables
Display Vegetables
Output:
Carrot
Tom got only carrot as a single (vegetable name) output. This is because Vegetables is a variable and it could store only single value at a time. Hence it replaced the other two and remembers only the carrot.
The correct sequence for Tom’s Vegetables list is as follows:
Hey Computer:
Create a list of my_vegetables
Remember “Cucumber” as my_vegetables
Remember “Lady Finger” as my_vegetables
Remember “Carrot” as my_vegetables
Display my_vegetables
Output:
Cucumber
Lady Finger
Carrot
Here instead of having three variables that store single vegetable, Tom can make a list that holds all the vegetables at one place.
Another example for lists is, suppose Tom wants to keep his best friends list at one place the correct sequence for that is as follows:
OK Computer[main()]:
Create a list of my_best_Friends
Remember “Dick” as my_best_Friends
Remember “Harry” as my_best_Friends
Remember “Johnny” as my_best_Friends
Display my_best_Friends
Output:
Dick
Harry
Johnny
In c language, if programmer wants to implement above task into his code form then below is the details:
main()
{
char *my_best_Friends[] = {"Dick", "Harry","Johnny"};
for (int i=0; i<3;i++)
puts(my_best_Friends[i]);
}
Output:
Dick
Harry
Johnny
So, it is concluded that lists are great place to keep a set of related values.
Summary
The lists are used to store a set of related values. Items of the list can be stored, rearranged, replaced and removed. The programmer can perform tasks on each item of the list together at once.
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