Академический Документы
Профессиональный Документы
Культура Документы
Digital DataPart 1
Table of Contents
Getting Acquainted with Digital DataPart 1 ......................................... 1
Session Objectives
Identify the different types of data types
Identify the types of variables
Identify the use of variables
Identifying the use of constants
Session Breakup
S. No. Duration
Section
(Minutes)
1 Introduction to Data Types 03
2 What are Data Types? 07
3 Different Data Types Available for 08
Programming
4 Introduction to Variables 05
5 What are Variables? 05
7 Naming Conventions and Rules for Variables 05
8 Types of Variables 05
9 Variables and Data Types 02
10 Introduction to Constants 05
11 Using Constants 05
12 Check Your Understanding 07
13 Summary 03
Total 60 minutes
For example, while writing a program for Student Management System, you have values such as
First Name, Last Name, Age, Height, and so on. If you associate a data type with these values, you
can restrict the type of operations that can be performed on it.
Different data types are used for storing different types of data. Each data type provides different
storage space and format. The basic data types allow computers to use the minimum memory
necessary for storing values, such as the Char data type uses 8 bits of memory for storing values, Int
uses 16 bits and Float uses 32 bits. However, this memory storage varies from one programming
language to the other.
Consider data types as your sorting boxes. You sort your data into a box depending on the type of
data. The alphabets and special symbols are put in the characters box, while the numbers are put in
the integer box.
The following figure shows the sorting of data into appropriate data types:
As a software developer, it is important to use the correct data type to ensure optimal use of memory
and storage. In addition, referencing correct data types ensures error-free program execution.
storage values and storing a smaller value will result in wastage of memory space. Moreover, since
the Double data type is made for larger values, it might take longer to perform calculations compared
to the Int data type.
Similarly, if you have to store salary where precision value is also required, then the Int data type will
not help as the salary may be in five figures or above. In such a situation, you will have to use the
Float data type.
Self-Read
To read more on the classification of data types in languages such as C, C++ and so on, visit
the following link:
http://www.tutorialspoint.com/cprogramming/c_data_types.htm
Another commonly used data type in programming languages is a composite data type called an
array. An array is an abstract data type that defines a block of contiguous memory locations, or data
which is a list of variables. The entire block is named, which becomes the reference to the array or
acts as the name of the array. The following figure represents an array with a name, Employees,
having four data values:
Self-Read
To read about abstract data types in languages such as C, visit the following link:
http://inst.eecs.berkeley.edu/~selfpace/studyguide/9C.sg/Output/ADTs.in.C.html
Introduction to Variables
As a developer, you need to understand the concept of variables along with its naming rules to
enable you to reference the memory locations and data in an effective manner. Variables refer to the
memory location that has a name assigned to it and store values that are subjected to manipulation
during the execution of program.
In the preceding figure, the variables, Number1 and Number2, can have different values based on
different inputs taken by developers.
In other words, the data type of the variables will be int, as shown in the following figure:
Variables with appropriate and relevant names in an application make the program easy to read and
understand.
For example, in an application that calculates employees salary, the variable names, such as
Emp_Salary and Sal_Emp, make the program easy to understand whenever there is a reference to
employees. On the other hand, if you give variable names, such as var1 and var2, the readability of
the program is hampered and it becomes difficult to understand what kind of value the variable var1
holds.
Variables make code adaptable to later changes in the code.
The following figure shows the correct variable names for the date of birth of an employee:
On the other hand, the following figure shows some incorrect variable names for the date of birth of
an employee:
Self-Read
To read more about variables and constants, visit the following link:
http://www.slideshare.net/KamalAcharya/data-types-and-variables
Types of Variables
Variables can be of two types:
Local variables: These are the variables available only to the program, function or code
snippet where they have been defined.
Global variables: These are the variables accessible to all functions/modules within an
application.
As a software developer, you need to understand the significance of variables and the type of
variable to be used in different scenarios.
For example, you are creating an application that has a main program and some small sub-programs
corresponding to different functions of the application. If you want to protect certain data such that it is
available only to one sub-program and is not accessible to any other program within the application,
you should declare the data with local variables.
On the other hand, if you want to make the data accessible to all sub-programs/functions/code
snippets within an application, then you should declare the data with global variables. Thus, ensuring
which data can be modified at any time, by any function and which data can be modified by only a
specific function.
The following figure shows the different types of variables in terms of an application:
Self-Read
To read more about local and global variables, visit the following link:
http://en.wikibooks.org/wiki/A-
level_Computing/AQA/Problem_Solving,_Programming,_Data_Representation_and_Practica
l_Exercise/Fundamentals_of_Programming/Global_and_Local_Variables
http://pages.cs.wisc.edu/~calvin/cs110/LOCAL_GLOBAL.html
Introduction to Constants
Constants refer to the memory location that has a name assigned to it and has a fixed value during
the execution of a program. As a developer, you need to understand the concept of constants to be
able to reference the memory locations and data in an effective manner.
Using Constants
Constants are used to store fixed data that is not changed during the execution of program.
Variables are used to store the data that can be changed during the
execution of a program, whereas constants are used to store data that is
fixed and cannot be changed during the execution of a program.
Consider the following example to understand the difference between variables and constants:
In a schools Student Administration application, for a particular class, the marks obtained by students
are a variable, whereas the class name for all the students, such as Class V, is a constant that will
not be changed during the execution of the program.
The following figure shows the difference between variables and constants for the preceding
example:
Self-Read
Question 4: You have to create an application to record the exact distance between two locations,
with precision up to 2 digits after decimal. You need to store the distance in a variable called
distance_measured. Which of the following data types should you use for the variable
distance_measured?
a. Int
b. Float
c. Char
d. Boolean
Question 5: What is the difference between a global variable and a local variable?
Question 6: You are creating an employee database. The application consists of a main program to
accept the employee data and a sub-program to calculate the employees salary and bonus. The
main program accepts employee details such as employee name, employee age and employees
date of joining. The sub-program calculates the employees salary and bonus using his/her date of
joining. You have created the following variable names corresponding to the data:
Question 9: You have to create an application to calculate the circumference of a circle. The formula
to calculate the circumference of a circle is:
Summary
In this session, you learned:
A data type is a predefined characteristic of a value.
Different data types are used for storing different types of data. Each data type provides
different storage space and format.
A data type determines:
The possible values for that type
The operations that can be done on values of that type
The way values of that type can be stored
Variables are used to temporarily store data at run time. A variable has a name and a data
type.
The name of a variable can consist of letters, digits and an underscore character. It must
begin either with a letter or with an underscore.
Variables are of two types:
Local
Global
The variable name is used to access and manipulate the value of a variable.
Constants are used to store fixed data that is not changed during the execution of a
program.
Question 2:
The correct order is:
Char < Int < Long < Double
Question 3:
The correct option is a.
The class refers to the number from 1 to 5 and will be a whole number without any decimal, so the
data type that you should use is Int.
Question 4:
The correct option is b.
distance_measured is a numerical value that may be up to 2 digits after decimal.
Question 5:
Local variables are the variables available only to the main program or function, whereas global
variables are accessible to all functions/modules within an application.
Question 6:
The correct option is c.
The sub-program calculating the employees salary and bonus uses employees date of joining for
salary calculation. Therefore, the variable to be defined in the main program as a global variable is
Emp_DOJ as it needs to be accessible to the sub-program.
Question 7:
a. Student__ Correct
b. ST@@ Incorrect as no special characters are allowed.
c. __Empname Correct
d. Age_1234_ Correct
e. Year_&&_1 Incorrect as no special characters are allowed.
f. Emp_DOB Correct
g. __%%employee_DOJ Incorrect as no special characters are allowed.
h. Number_1& Incorrect as no special characters are allowed.
i. Subject_Topic Correct
j. _1_Sub Correct
Question 8:
Variables are used to store the data that can be changed during the execution of a program. On the
other hand, constants are used to store data that is fixed and cannot be changed during the execution
of a program.
Question 9:
The correct option is a.
Since the value of circumference of circle and radius can be manipulated in application, these should
be defined as variables. On the other hand, the value of pie will always be fixed (3.14), irrespective of
any application using the value. Therefore, it should be defined as a constant with the value 3.14.
Question 10: