SE Computer- Division A
Course Name :Principles of Programming Language
Course Code: 210256
Course InCharge: Parag Achaliya
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Unit 2
Structuring the Data, Computations and Program
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Data type: A data type defines a collection of data values and
a set of predefined operations on those values
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
An object represents an instance of a user-defined (abstract data) type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Primitive Data Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Primitive Data Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Numerical Data Types …Integer
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Numerical Data Types …Floating Point
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Numerical Data Types …Floating Point
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Numerical Data Types …Decimal
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Boolean Types
Advantage: � Readability
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Character Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Character String Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Character String Types …. String Operations
Character string type is one in which the values consist of sequences of characters
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Character String Types.. String Length Options
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Character String Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Used Defined Ordinal Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Enumeration Types
enum days {mon, tue, wed, thu, fri, sat, sun};
Design issues
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Subrange Type
An ordered contiguous subsequence of an ordinal type
Example: 12..18 is a subrange of integer type
Introduced by Pascal and included in Ada
Ada’s design
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
subtype Weekdays is Days range Mon..Fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1; //legal if Day1 it not set to Sat or Sun
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array Type
An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element.
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array and Indexes
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array and Indexes
Language | Index Type |
FORTRAN , C, Java | Integer |
PASCAL | Any Ordinal Type (Integer, Boolean, Enumeration, Character) |
Ada | Integer Or Enumeration |
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Categories of Arrays
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Categories of Arrays
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array Initialization
Some language allow initialization at the time of storage allocation
List (3) Data List /0, 5, 5/ // List is initialized to the values
int list [] = {4, 5, 7, 83}
char name [] = “freddie”; // eight elements, including last element as null character
char *names [] = {“Bob”, “Jake”, “Joe”];
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array Initialization
String[] names = {“Bob”, “Jake”, “Joe”};
List : array (1..5) of Integer := (1, 3, 5, 7, 9);
Bunch : array (1..5) of Integer:= (1 => 3, 3 => 4, others => 0);
Note: the array value is (3, 0, 4, 0, 0)
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Array Operations
Language | Array Operation |
C-based | No Operations , only through methods |
C#, Perl | Array Assignments |
Ada | Assignment, Concatenation(&), Comparison |
Python | Concatenation(+), Element Membership(in), Comparison(==, is) |
FORTRAN | Matrix Multiplication, Transpose |
APL(Most powerful) | +.* , |
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Associative Array
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Associative Array
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Record Type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Record Type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Union
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Pointer and reference type
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Pointer Operations
j = *ptr
sets j to the value located at ptr
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Pointer Assignment Illustrated
The assignment operation
j = *ptr
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
39
Introduction to Expression
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
40
Arithmetic Expression
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Unary Arithmetic Operators in C/C++ and Java
41
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Unary Arithmetic Operators in C/C++ and Java (cont.)
42
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Binary Arithmetic Operators in C/C++ and Java
Operation | Operator | Example | Operand | Result |
Addition | + | A + B | Both integers ------------------- One operand is not integer | Integer ------------------ Double precision floating-point |
Subtraction | - | A – B | Both integers ---------------------------- One operand is not integer | Integer ----------------------- Double precision floating-point |
Multiplication | * | A * B | Both integers ------------------------------ One operand is not integer | Integer ------------------------- Double precision floating-point |
Division | / | A / B | Both integers ------------------------------ One operand is not integer | Integer ------------------------- Double precision floating-point |
Modulus (Remainder) | % | A % B | Both integers | Integer |
43
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Ternary Operator in C/C++ and Java
A ternary operator “ ?: ” has three operands.
average = (a>b)? a : b;
if (a>b) average = a;
else average = b;
44
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Arithmetic Expressions :Conditional Expressions
average = (a>b)? a : b;
if (a>b) average = a;
else average = b;
45
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Overloaded Operators
46
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Overloaded Operators - division operator issue
47
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Explicit Type Conversions
int a, b;� float x;� x = (float)a/(float)b;
48
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Relational and Boolean Expressions
49
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Short-Circuit Evaluation
If a is zero, there is no need to evaluate (b/13-1).
50
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Assignment Statements
<target_var> <assign_operator> <expression>
= FORTRAN, BASIC, PL/I, C, C++, Java.
:= ALGOLs, Pascal, Ada.
51
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Assignment statements (cont.)
while ((ch = getchar()) != EOF) { … }
ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement.
52
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Selection Statements
1-53
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Iterative Statements
1. How is iteration controlled?
2. Where is the control mechanism in the loop?
1-54
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
while (ctrl_expr)
loop body
do
loop body
while (ctrl_expr)
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Fundamentals of Subprograms
1-56
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Fundamentals of Subprograms
1-57
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Basic Definitions
1-58
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Basic Definitions
1-59
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
1-60
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Design Issues for Subprograms
1-61
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Local Referencing Environments
1-62
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Parameter Passing Methods
1-63
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Abstract Data Type and Encapsulation Constructs
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
The Concept of Abstraction
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Introduction to Data Abstraction
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Design Issues for Abstract Data Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Parameterized Abstract Data Types
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Encapsulation Constructs
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
Naming Encapsulations
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Department of Computer Engineering