Вы находитесь на странице: 1из 18

Getting Acquainted with

Digital DataPart 1
Table of Contents
Getting Acquainted with Digital DataPart 1 ......................................... 1

Appendix: Solutions to Check Your Understanding ........................... 15


Getting Acquainted with Digital Data

Getting Acquainted with Digital DataPart 1


Session Overview
This session explores the different data types used for programming. The session also discusses the
need and use of variables. In addition, the session explains how to associate variables with
appropriate names and data types. Later, the session explores the difference between variables and
constants used in programming.

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

NIIT Yuva Jyoti


1
Getting Acquainted with Digital Data

Introduction to Data Types


A program consists of various components. These components define the way values are accepted in
the program, the way values are computed or calculated within the program, and the way the result is
displayed by the program. The following figure shows these different components of a program:

This session discusses data types, variables and constants.


In programming, you use different data types for classification of information. A data type is a
predefined characteristic of a value. As a developer, you need to analyse the application to identify
the data type of each value, which the application will be referencing. For example, in a Student
Administration application, you would need to identify the type of values you would need to store and
compute, such as student name, date of birth, marks, overall average and so on. In this application,
student name would be characters while marks and overall average would be numbers.
The following figure shows various types of data classification:

What are Data Types?


A data type defines the type of value that can be stored in a program for later use. A data type is a
critical part of all programming languages, including C, C++, Java and so on. Data types help the
programmer to define the type of values to predict the type of operations performed on it.

NIIT Yuva Jyoti


2
Getting Acquainted with Digital Data

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:

NIIT Yuva Jyoti


3
Getting Acquainted with Digital Data

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

A data type is used to identify various types of data, such as floating-point,


integer or Boolean.

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.

Different Data Types Available for Programming


There are different data types available for programming. Each data type has different memory size
and value range.
Data types can be of the following types:
Logical data type such as Boolean
Text data type such as char and string
Numeric data type such as int and float
The following figure shows different data types along with their examples for usage:

Consider the following example for using data types.


You are creating an application for an organisation to create an employee database wherein you
would want to take reference of an employees years of experience to calculate his/her salary. The
range of employees experience is 1-10 years.
If you have to store the years of experience, which can be a number between 1 and 10, then you can
use the Int data type. You should not use the Double data type as this data type is defined for larger

NIIT Yuva Jyoti


4
Getting Acquainted with Digital Data

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.

NIIT Yuva Jyoti


5
Getting Acquainted with Digital Data

Consider the following example for understanding the variables.


To add two numbers, you need two variables to hold these numbers. The memory location will hold
the data in the form of numbers. The names assigned to the memory location are Number1 and
Number2, which are variables.
The following figure shows the memory location and the data associated with this memory location:

In the preceding figure, the variables, Number1 and Number2, can have different values based on
different inputs taken by developers.

What are Variables?


Variables are used to store data at runtime. A variable has a name and a data type associated with it.
The variable name is used to access and manipulate the value of the variable. The data type of a
variable determines the type of data that the variable can store.
Considering the previous example, the two memory locations referred by the variables Number1 and
Number2 will store the number as the data.

NIIT Yuva Jyoti


6
Getting Acquainted with Digital Data

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.

Naming Conventions and Rules for Variables


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.
Consider the following example for naming variables:
You are creating an employee database program, and you need to create a variable for the date of
birth of an employee.

NIIT Yuva Jyoti


7
Getting Acquainted with Digital Data

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:

NIIT Yuva Jyoti


8
Getting Acquainted with Digital Data

While naming variables in a programming language, you need to ensure


that you dont use the keywords defined for that language as variable
names

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:

NIIT Yuva Jyoti


9
Getting Acquainted with Digital Data

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

Variables and Data Types


The variable name is used to access and manipulate the value of the variable. The data type of a
variable determines the type of data that a variable can store.
Consider the following example for variables and data types:
You have to create an application for Employees Payroll System of an organisation. For this, you
have to create variables such as Emp_Name and Pan_Card_No. The Emp_Name variable will
contain only alphabets as the value, while the Pan_Card_No variable will contain alphanumeric
values.
Similarly, you have to create an application to record the temperature of a room with correct
precision, every hour of the day. For this, you have to create variables such as Room_temp and
Hour_Of_Day. The Room_temp variable will contain a floating point or a double value as we need the
temperature with precision that is with decimal values, while the Hour_Of_Day variable will contain a
numeric value.
These variables need to be created in the beginning of the development of the application. On the
other hand, a variable such as bonus, which calculates the bonus of an employee, is created at a
later stage of the development of the application. Thus, as a software developer, you will create
variables and initialise these at various stages of software development.

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.

NIIT Yuva Jyoti


10
Getting Acquainted with Digital Data

The following figure shows the different categories of constants:

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.

NIIT Yuva Jyoti


11
Getting Acquainted with Digital Data

The following figure shows the difference between variables and constants for the preceding
example:

Self-Read

To read more on constants, visit the following links:


http://www.tutorialspoint.com/cprogramming/c_constants.htm
http://cstl.syr.edu/fipse/GraphA/Unit1/Unit1.html

Check Your Understanding


Question 1: A data type determines ________________.
a. The possible values for that type
b. The mathematical operations that can be performed on values of that type
c. The time for which the values are stored for that type
d. The accessibility of the value for that type
Question 2: Arrange the following data types in ascending order of their sizes in bits.
a. Char
b. Long
c. Double
d. Int
Question 3: You have to create an annual school report application till fifth class. You need to store
the class (1-5) in a variable called class_number. Which of the following data types should you use
for the variable class_number?
a. Int
b. Float
c. Char
d. Double

NIIT Yuva Jyoti


12
Getting Acquainted with Digital Data

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:

Data Variable Name


Employee Name Emp_Name
Employee Age Emp_Age
Employee Date Of Joining Emp_DOJ
Employee Salary Emp_Sal
Bonus Bonus
Which of the following variables should be defined as a global variable?
a. Emp_Name
b. Emp_Age
c. Emp_DOJ
d. Emp_Sal
e. Bonus
Question 7: Mark the following variable name as correct or incorrect based on the naming
conventions for variables.
a. Student__
b. ST@@
c. __Empname
d. Age_1234_
e. Year_&&_1
f. Emp_DOB
g. __%%employee_DOJ
h. Number_1&
i. Subject_Topic
j. _1_Sub
Question 8: What is the difference between a variable and a constant?

NIIT Yuva Jyoti


13
Getting Acquainted with Digital 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:

Circumference of Circle = 2 * * Radius of Circle

Where the value of (Pie) = 3.14.


Consider the preceding example and identify the statement that is true regarding efficient
programming.
a. Define the circumference of circle and radius as variable, and Pie as constant.
b. Define the circumference of circle and radius as constant, and Pie as variable.
c. Define the circumference of circle as variable and radius, and Pie as constant.
d. Define the circumference of circle, radius and Pie as constant.
Question 10: Match the following:

A Data type 1 Is available to only the main program or function.


B Variable 2 Is used to define buckets for storing different types of
data
C Constants 3 Is subject to manipulation during the execution of a
program.
D Global variable 4 Accessible to all functions within an application.

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.

NIIT Yuva Jyoti


14
Getting Acquainted with Digital Data

Appendix: Solutions to Check Your Understanding


Question 1:
A data type determines the possible values for that type.

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

NIIT Yuva Jyoti


15
Getting Acquainted with Digital Data

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:

A Data type 2 Is used to define buckets for storing different


types of data
B Variable 3 Is subject to manipulation during the execution
of a program.
C Constants 1 Is available only to the main program or
function.
D Global variable 4 Accessible to all functions within an
application.

NIIT Yuva Jyoti


16

Вам также может понравиться