SlideShare a Scribd company logo
1
Programming Languages &Programming Languages &
StructureStructure
University of Education Okara Campus
UniversityofEducationOkara
Campus
Part 3
2
Data types and Variables
A data type defines a set of values and a set of operations that can be applied
to those values. The set of values for each type is known as the domain for the
type.
UniversityofEducationOkara
Campus
Variables are names for memory locations. As discussed in Chapter 5, each
memory location in a computer has an address. Although the addresses are
used by the computer internally, it is very inconvenient for the programmer to
use addresses. A programmer can use a variable, such as score, to store the
integer value of a score received in a test. Since a variable holds a data item, it
has a type.
e.g. int a
Here INT is a data type and A is a variable
3
Literals
A literal is a predetermined value used in a program. For example,
if we need to calculate the area of circle when the value of the
radius is stored in the variable r, we can use the expression 3.14 ×
r2
, in which the approximate value of π (pi) is used as a literal. In
most programming languages we can have integer, real, character
and Boolean literals. In most languages, we can also have string
literals. To distinguish the character and string literals from the
names of variables and other objects, most languages require that
the character literals be enclosed in single quotes, such as 'A', and
strings to be enclosed in double quotes, such as "Anne".
UniversityofEducationOkara
Campus
The use of literals is not considered good programming practice
unless we are sure that the value of the literal will not change
with time (such as the value of π in geometry). However, most
literals may change value with time.
4
For this reason, most programming languages define constants. A
constant, like a variable, is a named location that can store a value, but the
value cannot be changed after it has been defined at the beginning of the
program. However, if we want to use the program later, we can change just
one line at the beginning of the program, the value of the constant.
e.g. int a = 10;
UniversityofEducationOkara
Campus
Inputs and Outputs
Almost every program needs to read and/or write data. These operations can
be quite complex, especially when we read and write large files. Most
programming languages use a predefined function for input and output.
cout for printing (output)
cin for scanning (input)
5
Expressions
An expression is a sequence of operands and operators that reduces to a single
value. For example, the following is an expression with a value of 13:
An operator is a language-specific token that requires an action to
be taken. The most familiar operators are drawn from mathematics.
UniversityofEducationOkara
Campus
Table 9.3 shows some arithmetic operators used in C, C++, and Java.
6
UniversityofEducationOkara
Campus
Logical operators combine Boolean values (true or false) to get a
new value. The C language uses three logical operators, as shown
in Table 9.5:
Relational operators compare data to see if a value is greater than, less than, or
equal to another value. The result of applying relational operators is a Boolean
value (true or false). C, C++ and Java use six relational operators, as shown in
Table 9.4:
7
Figure 10 Three types of repetition
UniversityofEducationOkara
Campus
8
Pass by value
In parameter pass by value, the main program and the subprogram
create two different objects (variables). The object created in the
program belongs to the program and the object created in the
subprogram belongs to the subprogram. Since the territory is
different, the corresponding objects can have the same or different
names. Communication between the main program and the
subprogram is one-way, from the main program to the
subprogram.
UniversityofEducationOkara
Campus
Example 1
Assume that a subprogram is responsible for carrying out printing
for the main program. Each time the main program wants to print
a value, it sends it to the subprogram to be printed. The main
program has its own variable X, the subprogram has its own
variable A. What is sent from the main program to the
subprogram is the value of variable X.
9
Figure 12 An example of pass by value
UniversityofEducationOkara
Campus
Example 2
In Example 1, since the main program sends only a value to the subprogram, it
does not need to have a variable for this purpose: the main program can just
send a literal value to the subprogram. In other words, the main program can
call the subprogram as print (X) or print (5).
Example 3
Assume that the main program has two variables X and Y that need to swap
their values. The main program passes the value of X and Y to the
subprogram, which are stored in two variables A and B. The swap subprogram
uses a local variable T (temporary) and swaps the two values in A and B, but
the original values in X and Y remain the same: they are not swapped.
10
An example of pass by value in real life is when a friend wants to
borrow and read a valued book that you wrote. Since the book is
precious, possibly out of print, you make a copy of the book and
pass it to your friend. Any harm to the copy therefore does not
affect the original book.
UniversityofEducationOkara
Campus
Example 4
Figure 13 An example in which pass by value does not work
Returning values
A subprogram can be designed to return a value or values. This is the way that
predefined procedures are designed. When we use the expression C ← A + B,
we actually call a procedure add (A, B) that returns a value to be stored in the
variable C.
11
UniversityofEducationOkara
Campus
Pass by reference
Pass by reference was devised to allow a subprogram to change the value of a
variable in the main program. In pass by reference, the variable, which in
reality is a location in memory, is shared by the main program and the
subprogram. The same variable may have different names in the main program
and the subprogram, but both names refer to the same variable. Metaphorically,
we can think of pass by reference as a box with two doors: one opens in the
main program, the other opens in the subprogram. The main program can leave
a value in this box for the subprogram, the subprogram can change the original
value and leave a new value for the program in it.
Example 5
If we use the same swap subprogram but let the variables be
passed by reference, the two values in X and Y are actually
exchanged.
Figure 14 An example of pass by reference

More Related Content

PPT
Introduction to programing languages part 2
PDF
Object oriented concepts ppt
PPT
Programming In C++
PDF
Java chapter 3
PPTX
Procedural programming
PPTX
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
PPT
Object oriented vs. object based programming
Introduction to programing languages part 2
Object oriented concepts ppt
Programming In C++
Java chapter 3
Procedural programming
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
Object oriented vs. object based programming

What's hot (20)

PPTX
Language design and translation issues
PPT
PDF
Introduction to c++ ppt 1
PPT
Introduction to programming languages part 2
PPT
Bca 2nd sem u-1 iintroduction
PPTX
Procedural vs. object oriented programming
PPTX
Principal of objected oriented programming
PPTX
Principles of object oriented programming
PPTX
Presentation c
PPT
Object Oriented Technologies
PPTX
Introduction to object oriented language
PPTX
Object Oriented programming - Introduction
PPTX
Object Oriented Programming
PPTX
Ak procedural vs oop
PPTX
diffrence between procedure oriented programming & object oriented programmin...
PPTX
POP vs OOP Introduction
PPTX
Compare between pop and oop
PPTX
C++ with student management system project
DOC
Programming paradigms
PPT
Object Oriented Language
Language design and translation issues
Introduction to c++ ppt 1
Introduction to programming languages part 2
Bca 2nd sem u-1 iintroduction
Procedural vs. object oriented programming
Principal of objected oriented programming
Principles of object oriented programming
Presentation c
Object Oriented Technologies
Introduction to object oriented language
Object Oriented programming - Introduction
Object Oriented Programming
Ak procedural vs oop
diffrence between procedure oriented programming & object oriented programmin...
POP vs OOP Introduction
Compare between pop and oop
C++ with student management system project
Programming paradigms
Object Oriented Language
Ad

Viewers also liked (7)

PDF
American institute for Professional Studies Profile
PPTX
COMPANY PROFILE
PPTX
Process Oriented Layout
PPTX
Product and Process Layout
PPT
Tn6 facility layout
PPT
Unit 1.3 Introduction to Programming (Part 3)
PPT
Unit 1.3 Introduction to Programming (Part 1)
American institute for Professional Studies Profile
COMPANY PROFILE
Process Oriented Layout
Product and Process Layout
Tn6 facility layout
Unit 1.3 Introduction to Programming (Part 3)
Unit 1.3 Introduction to Programming (Part 1)
Ad

Similar to Introduction to programing languages part 3 (20)

PDF
谷歌留痕👉seo233.com👈
PDF
谷歌留痕👉seo233.com👈
PDF
ismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdf
PDF
C_Program_Yr1[1].pdf for computer science
PDF
C programming course material
PPTX
Module 4.pptx
PPTX
PROGRAMMING, the presentation is about,,
DOCX
Oo ps exam answer2
PPT
Introduction to Procedural Programming in C++
PDF
Book management system
PDF
Pattern-Level Programming with Asteroid
PPT
PROGRAMMING LANGUAGES
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PDF
C Programming Slides for 1st Year Engg students
DOCX
PYTHON NOTES
 
PPT
Programming Languages An Intro
PDF
PPTX
programming for problem solving in C and C++.pptx
谷歌留痕👉seo233.com👈
谷歌留痕👉seo233.com👈
ismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdfismm2009.pdf
C_Program_Yr1[1].pdf for computer science
C programming course material
Module 4.pptx
PROGRAMMING, the presentation is about,,
Oo ps exam answer2
Introduction to Procedural Programming in C++
Book management system
Pattern-Level Programming with Asteroid
PROGRAMMING LANGUAGES
C++ tutorial assignment - 23MTS5730.pptx
C Programming Slides for 1st Year Engg students
PYTHON NOTES
 
Programming Languages An Intro
programming for problem solving in C and C++.pptx

More from university of education,Lahore (20)

PPT
Activites and Time Planning
PPT
Classical Encryption Techniques
PPT
Activites and Time Planning
PPTX
OSI Security Architecture
PPTX
Network Security Terminologies
PPT
Project Scheduling, Planning and Risk Management
PPTX
Software Testing and Debugging
PPTX
PPT
Enterprise Application Integration
PPTX
PPTX
Itertaive Process Development
PPTX
Computer Aided Software Engineering Nayab Awan
PPTX
Lect 2 assessing the technology landscape
PPTX
system level requirements gathering and analysis
Activites and Time Planning
Classical Encryption Techniques
Activites and Time Planning
OSI Security Architecture
Network Security Terminologies
Project Scheduling, Planning and Risk Management
Software Testing and Debugging
Enterprise Application Integration
Itertaive Process Development
Computer Aided Software Engineering Nayab Awan
Lect 2 assessing the technology landscape
system level requirements gathering and analysis

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Computing-Curriculum for Schools in Ghana
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
A systematic review of self-coping strategies used by university students to ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Lesson notes of climatology university.
master seminar digital applications in india
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Introduction to programing languages part 3

  • 1. 1 Programming Languages &Programming Languages & StructureStructure University of Education Okara Campus UniversityofEducationOkara Campus Part 3
  • 2. 2 Data types and Variables A data type defines a set of values and a set of operations that can be applied to those values. The set of values for each type is known as the domain for the type. UniversityofEducationOkara Campus Variables are names for memory locations. As discussed in Chapter 5, each memory location in a computer has an address. Although the addresses are used by the computer internally, it is very inconvenient for the programmer to use addresses. A programmer can use a variable, such as score, to store the integer value of a score received in a test. Since a variable holds a data item, it has a type. e.g. int a Here INT is a data type and A is a variable
  • 3. 3 Literals A literal is a predetermined value used in a program. For example, if we need to calculate the area of circle when the value of the radius is stored in the variable r, we can use the expression 3.14 × r2 , in which the approximate value of π (pi) is used as a literal. In most programming languages we can have integer, real, character and Boolean literals. In most languages, we can also have string literals. To distinguish the character and string literals from the names of variables and other objects, most languages require that the character literals be enclosed in single quotes, such as 'A', and strings to be enclosed in double quotes, such as "Anne". UniversityofEducationOkara Campus The use of literals is not considered good programming practice unless we are sure that the value of the literal will not change with time (such as the value of π in geometry). However, most literals may change value with time.
  • 4. 4 For this reason, most programming languages define constants. A constant, like a variable, is a named location that can store a value, but the value cannot be changed after it has been defined at the beginning of the program. However, if we want to use the program later, we can change just one line at the beginning of the program, the value of the constant. e.g. int a = 10; UniversityofEducationOkara Campus Inputs and Outputs Almost every program needs to read and/or write data. These operations can be quite complex, especially when we read and write large files. Most programming languages use a predefined function for input and output. cout for printing (output) cin for scanning (input)
  • 5. 5 Expressions An expression is a sequence of operands and operators that reduces to a single value. For example, the following is an expression with a value of 13: An operator is a language-specific token that requires an action to be taken. The most familiar operators are drawn from mathematics. UniversityofEducationOkara Campus Table 9.3 shows some arithmetic operators used in C, C++, and Java.
  • 6. 6 UniversityofEducationOkara Campus Logical operators combine Boolean values (true or false) to get a new value. The C language uses three logical operators, as shown in Table 9.5: Relational operators compare data to see if a value is greater than, less than, or equal to another value. The result of applying relational operators is a Boolean value (true or false). C, C++ and Java use six relational operators, as shown in Table 9.4:
  • 7. 7 Figure 10 Three types of repetition UniversityofEducationOkara Campus
  • 8. 8 Pass by value In parameter pass by value, the main program and the subprogram create two different objects (variables). The object created in the program belongs to the program and the object created in the subprogram belongs to the subprogram. Since the territory is different, the corresponding objects can have the same or different names. Communication between the main program and the subprogram is one-way, from the main program to the subprogram. UniversityofEducationOkara Campus Example 1 Assume that a subprogram is responsible for carrying out printing for the main program. Each time the main program wants to print a value, it sends it to the subprogram to be printed. The main program has its own variable X, the subprogram has its own variable A. What is sent from the main program to the subprogram is the value of variable X.
  • 9. 9 Figure 12 An example of pass by value UniversityofEducationOkara Campus Example 2 In Example 1, since the main program sends only a value to the subprogram, it does not need to have a variable for this purpose: the main program can just send a literal value to the subprogram. In other words, the main program can call the subprogram as print (X) or print (5). Example 3 Assume that the main program has two variables X and Y that need to swap their values. The main program passes the value of X and Y to the subprogram, which are stored in two variables A and B. The swap subprogram uses a local variable T (temporary) and swaps the two values in A and B, but the original values in X and Y remain the same: they are not swapped.
  • 10. 10 An example of pass by value in real life is when a friend wants to borrow and read a valued book that you wrote. Since the book is precious, possibly out of print, you make a copy of the book and pass it to your friend. Any harm to the copy therefore does not affect the original book. UniversityofEducationOkara Campus Example 4 Figure 13 An example in which pass by value does not work Returning values A subprogram can be designed to return a value or values. This is the way that predefined procedures are designed. When we use the expression C ← A + B, we actually call a procedure add (A, B) that returns a value to be stored in the variable C.
  • 11. 11 UniversityofEducationOkara Campus Pass by reference Pass by reference was devised to allow a subprogram to change the value of a variable in the main program. In pass by reference, the variable, which in reality is a location in memory, is shared by the main program and the subprogram. The same variable may have different names in the main program and the subprogram, but both names refer to the same variable. Metaphorically, we can think of pass by reference as a box with two doors: one opens in the main program, the other opens in the subprogram. The main program can leave a value in this box for the subprogram, the subprogram can change the original value and leave a new value for the program in it. Example 5 If we use the same swap subprogram but let the variables be passed by reference, the two values in X and Y are actually exchanged. Figure 14 An example of pass by reference