SlideShare a Scribd company logo
1 of 22
CHAPTER 4
VARIABLES AND DATA
TYPES
PROG0101
FUNDAMENTALS OF
PROGRAMMING
CHAPTER 3
ALGORITHMS
PROG0101
FUNDAMENTALS OF PROGRAMMING
C H A P T E R 1
O V E R V I E W O F C O M P U T E R S A N D L O G I C
PROG0101
FUNDAMENTALS OF
PROGRAMMING
PROG0101
FUNDAMENTALS OF PROGRAMMING
CHAPTER 4
VARIABLES
2
Introduction to Variables
 A symbol or name that stands for a value.
 A variable is a value that can change.
 Variables provide temporary storage for
information that will be needed during the lifespan
of the computer program (or application).
3
Introduction to Variables
Example:
 This is an example of programming expression.
 x, y and z are variables.
 Variables can represent numeric values, characters,
character strings, or memory addresses.
z = x + y
4
Introduction to Variables
 Variables store everything in your program.
 The purpose of any useful program is to modify
variables.
 In a program every, variable has:
 Name (Identifier)
 Data Type
 Size
 Value
5
Types of Variables
 There are two types of variables:
 Local variable
 Global variable
6
Types of Variables
 Local variables are those that are in scope within a
specific part of the program (function, procedure,
method, or subroutine, depending on the
programming language employed).
 Global variables are those that are in scope for the
duration of the programs execution. They can be
accessed by any part of the program, and are read-
write for all statements that access them.
7
Types of Variables
Types of Variable
MAIN PROGRAM
Global Variables
Subroutine
Local
Variable
8
Rules in Naming a Variable
 There a certain rules in naming variables
(identifier).
 They are:
 It should use only alphabets, number and underscore ( _ )
 It should not begin with a number and must have at least
one alphabet.
 It should not be a reserved word.
 Examples of reserved word:
main long if do continue
short else return const int
double break void while char
9
Rules in Naming a Variable
 Examples of valid variable’s name:
 Examples of not valid variable’s name:
x number1 a123 number_2 _xyz
print main space not allowed
123number name should not begin with number
main reserved word
10
Constants
 The opposite of a variable is a constant.
 A constant is a value that never changes.
 Because of their inflexibility, constants are used less
often than variables in programming.
 A constant can be :
 a number, like 25 or 3.6
 a character, like a or $
 a character string, like "this is a string"
11
Constants
 Example:
 Variable:
 area, r
 Constant
 3.142
area = 3.142 * r * r
12
Data Types
 Data type is classification of a particular type of
information.
 Data types are essential to any computer
programming language.
 Without them, it becomes very difficult to maintain
information within a computer program.
 Different data types have different sizes in memory
depending on the machine and compilers.
13
Data Types
Examples of Data Types
 Integer
 Floating-point
 Character
 String
 Boolean
14
Data Types
Integer
 A whole number, a number that has no fractional part.
 The following are integers:
 In contrast, the following are not integers:
 There are often different sizes of integers available; for
example, PCs support short integers, which are 2 bytes,
and long integers, which are 4 bytes.
0 1 -125 144457
5.34 -1.0 1.3E4 "string"
15
Data Types
Floating-point
 A number with a decimal point.
 The following are floating-point numbers:
 Floating-point representations are slower and less
accurate than fixed-point representations, but they
can handle a larger range of numbers.
 It require more space.
3.0 -111.5
16
Data Types
Character
 In graphics-based applications, the term character
is generally reserved for letters, numbers, and
punctuation..
 It only can represent single character.
 The following are characters:
a A @ $ 2
17
Data Types
String
 String is a sequence of character.
 Example:
 Kuala Lumpur
 John White
 Computer
 Student011
18
Data Types
Boolean
 This data type only have two possible values:
 True
 False
 Use this data type for simple flags that track
true/false conditions.
19
Declaration of Variable
 In programming languages all the variables that a
program is going to use must be declared prior to
use.
 Declaration of a variable serves two purposes:
 It associates a type and an identifier (or name) with
the variable. The type allows the compiler to
interpret statements correctly.
 It allows the compiler to decide how much storage
space to allocate for storage of the value associated
with the identifier and to assign an address for each
variable which can be used in code generation.
20
Declaration of Variable
 A typical set of variable declarations that might
appear at the beginning of a program could be as
follows:
int count;
Data type Variable
21
Declaration of Variable
 Often in programming numerical constants are
used, e.g. the value of  (pi = 3.14).
 It is well worthwhile to associate meaningful names
with constants.
 These names can be associated with the appropriate
numerical value in a constant declaration.
 The names given to constants must conform to the
rules for the formation of identifiers as defined
above.
22
Declaration of Variable
 The following constant declaration:
 The general form of a constant declaration is:
const int days_in_year = 365;
const type constant-identifier = value ;

More Related Content

What's hot

pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#Prasanna Kumar SM
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++Neeru Mittal
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variablesPrem Kumar Badri
 
Dot net programming concept
Dot net  programming conceptDot net  programming concept
Dot net programming conceptsandeshjadhav28
 
Programming in C- Introduction
Programming in C- IntroductionProgramming in C- Introduction
Programming in C- Introductionsavitamhaske
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionEng Teong Cheah
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesMicheal Ogundero
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of CSuchit Patel
 

What's hot (20)

C tokens
C tokensC tokens
C tokens
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Dot net programming concept
Dot net  programming conceptDot net  programming concept
Dot net programming concept
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
 
Programming in C- Introduction
Programming in C- IntroductionProgramming in C- Introduction
Programming in C- Introduction
 
Typedef
TypedefTypedef
Typedef
 
Structure
StructureStructure
Structure
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 
Data types
Data typesData types
Data types
 

Similar to Variables and Data Types in Programming

Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAKTabsheer Hasan
 
Programming construction tools
Programming construction toolsProgramming construction tools
Programming construction toolssunilchute1
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptxmadhurij54
 
Semester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.pptSemester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.pptChetanChauhan203001
 
Introduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing studentsIntroduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing studentsAsfiya14
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfAbrehamKassa
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniquesvalarpink
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c languageAkshhayPatel
 

Similar to Variables and Data Types in Programming (20)

Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
 
Programming construction tools
Programming construction toolsProgramming construction tools
Programming construction tools
 
C
CC
C
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
Semester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.pptSemester-1-22-23-Introduction_to_c_programming.ppt
Semester-1-22-23-Introduction_to_c_programming.ppt
 
C.ppt
C.pptC.ppt
C.ppt
 
Introduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing studentsIntroduction_to_c_programming.ppt for software developing students
Introduction_to_c_programming.ppt for software developing students
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
 
C tokens
C tokensC tokens
C tokens
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 
C language ppt
C language pptC language ppt
C language ppt
 
C language
C languageC language
C language
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
Data type
Data typeData type
Data type
 

More from Mohd Harris Ahmad Jaal

Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Mohd Harris Ahmad Jaal
 

More from Mohd Harris Ahmad Jaal (20)

Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
 
Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
 
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2
 
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
 
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10
 
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9
 
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8
 
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7
 
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6
 
Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

Variables and Data Types in Programming

  • 1. CHAPTER 4 VARIABLES AND DATA TYPES PROG0101 FUNDAMENTALS OF PROGRAMMING CHAPTER 3 ALGORITHMS PROG0101 FUNDAMENTALS OF PROGRAMMING C H A P T E R 1 O V E R V I E W O F C O M P U T E R S A N D L O G I C PROG0101 FUNDAMENTALS OF PROGRAMMING PROG0101 FUNDAMENTALS OF PROGRAMMING CHAPTER 4 VARIABLES
  • 2. 2 Introduction to Variables  A symbol or name that stands for a value.  A variable is a value that can change.  Variables provide temporary storage for information that will be needed during the lifespan of the computer program (or application).
  • 3. 3 Introduction to Variables Example:  This is an example of programming expression.  x, y and z are variables.  Variables can represent numeric values, characters, character strings, or memory addresses. z = x + y
  • 4. 4 Introduction to Variables  Variables store everything in your program.  The purpose of any useful program is to modify variables.  In a program every, variable has:  Name (Identifier)  Data Type  Size  Value
  • 5. 5 Types of Variables  There are two types of variables:  Local variable  Global variable
  • 6. 6 Types of Variables  Local variables are those that are in scope within a specific part of the program (function, procedure, method, or subroutine, depending on the programming language employed).  Global variables are those that are in scope for the duration of the programs execution. They can be accessed by any part of the program, and are read- write for all statements that access them.
  • 7. 7 Types of Variables Types of Variable MAIN PROGRAM Global Variables Subroutine Local Variable
  • 8. 8 Rules in Naming a Variable  There a certain rules in naming variables (identifier).  They are:  It should use only alphabets, number and underscore ( _ )  It should not begin with a number and must have at least one alphabet.  It should not be a reserved word.  Examples of reserved word: main long if do continue short else return const int double break void while char
  • 9. 9 Rules in Naming a Variable  Examples of valid variable’s name:  Examples of not valid variable’s name: x number1 a123 number_2 _xyz print main space not allowed 123number name should not begin with number main reserved word
  • 10. 10 Constants  The opposite of a variable is a constant.  A constant is a value that never changes.  Because of their inflexibility, constants are used less often than variables in programming.  A constant can be :  a number, like 25 or 3.6  a character, like a or $  a character string, like "this is a string"
  • 11. 11 Constants  Example:  Variable:  area, r  Constant  3.142 area = 3.142 * r * r
  • 12. 12 Data Types  Data type is classification of a particular type of information.  Data types are essential to any computer programming language.  Without them, it becomes very difficult to maintain information within a computer program.  Different data types have different sizes in memory depending on the machine and compilers.
  • 13. 13 Data Types Examples of Data Types  Integer  Floating-point  Character  String  Boolean
  • 14. 14 Data Types Integer  A whole number, a number that has no fractional part.  The following are integers:  In contrast, the following are not integers:  There are often different sizes of integers available; for example, PCs support short integers, which are 2 bytes, and long integers, which are 4 bytes. 0 1 -125 144457 5.34 -1.0 1.3E4 "string"
  • 15. 15 Data Types Floating-point  A number with a decimal point.  The following are floating-point numbers:  Floating-point representations are slower and less accurate than fixed-point representations, but they can handle a larger range of numbers.  It require more space. 3.0 -111.5
  • 16. 16 Data Types Character  In graphics-based applications, the term character is generally reserved for letters, numbers, and punctuation..  It only can represent single character.  The following are characters: a A @ $ 2
  • 17. 17 Data Types String  String is a sequence of character.  Example:  Kuala Lumpur  John White  Computer  Student011
  • 18. 18 Data Types Boolean  This data type only have two possible values:  True  False  Use this data type for simple flags that track true/false conditions.
  • 19. 19 Declaration of Variable  In programming languages all the variables that a program is going to use must be declared prior to use.  Declaration of a variable serves two purposes:  It associates a type and an identifier (or name) with the variable. The type allows the compiler to interpret statements correctly.  It allows the compiler to decide how much storage space to allocate for storage of the value associated with the identifier and to assign an address for each variable which can be used in code generation.
  • 20. 20 Declaration of Variable  A typical set of variable declarations that might appear at the beginning of a program could be as follows: int count; Data type Variable
  • 21. 21 Declaration of Variable  Often in programming numerical constants are used, e.g. the value of  (pi = 3.14).  It is well worthwhile to associate meaningful names with constants.  These names can be associated with the appropriate numerical value in a constant declaration.  The names given to constants must conform to the rules for the formation of identifiers as defined above.
  • 22. 22 Declaration of Variable  The following constant declaration:  The general form of a constant declaration is: const int days_in_year = 365; const type constant-identifier = value ;