0% found this document useful (0 votes)
20 views

PPL notes Unit2

Uploaded by

anaghasalunke44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

PPL notes Unit2

Uploaded by

anaghasalunke44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT II.

Structuring the Data, Computations and Program

SYLLABUS CONTENT

Elementary Data Types : Primitive data Types, Character


String types, User Defined Ordinal Types, Array types,
Associative Arrays, Record Types, Union Types, Pointer and
reference Type.
Expression and Assignment Statements: Arithmetic
expression, Overloaded Operators, Type conversions, Relational
and Boolean Expressions, Short Circuit Evaluation, Assignment
Statements, Mixed mode Assignment.
Statement level Control Statements: Selection Statements,
Iterative Statements, Unconditional Branching.
Subprograms: Fundamentals of Sub Programs, Design Issues
for Subprograms, Local referencing Environments, Parameter
passing methods.
Abstract Data Types and Encapsulation Construct: Design
issues for Abstraction, Parameterized Abstract Data types,
Encapsulation Constructs, Naming Encapsulations

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 1
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

2.1 ELEMENTARY DATA TYPES

Data type: A data type defines a collection of data values and a set of predefined
operations on those values.

2.1.1 Primitive data Types


● Primitive data types are those that are not defined in terms of other data
types
● These are not built from other types

● These are atomic and cannot be decomposed further

● Some of the data types are merely reflections of the hardware - for eq. Integer
types
● Early PLs had only numeric primitive types, and still play a central role
among the collections of types supported by contemporary languages.

2.1.1.1 Types of Primitive Data Types


1. Numeric Types
2. Boolean Types
3. Character Types

1. Numeric Types
a. Integer

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 2
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

b. Floating Point

Newer Machines Use IEEE (The Institute of Electrical and


Electronics Engineers) floating-point formats: (a) Single precision, (b)
Double precision

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 3
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

c. Complex
● Represented as pair of real and imaginary part
● Imaginary Part is represented as literal such as i or j
● Ex. 5+7i is a complex number.

d. Decimal

Advantage

○ Accuracy

● Disadvantages:

○ Limited range

○ Wastes memory
2. Boolean Types

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 4
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

Advantage: Readability
3. Character Type

2.1.1.2 Type Structure in C++

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 5
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

i) Built-In Data Types

The basic (fundamental) data types provided by c++ are integral, floating point
and void data type.

Among these data types, the integral and floating-point data types can be
preceded by several type modifiers. These modifiers (also known as type qualifiers)
are the keywords that alter either size or range or both of the data types. The
various modifiers are short, long, signed and unsigned. By default the modifier is
signed.

In addition to these basic data types, ANSI C++ has introduced two more data
types namely, bool and wchar_t.

i. Integral Data Type: The integral data type is used to store integers and includes
char (character) and int (integer) data types.

a. Char: Characters refer to the alphabet, numbers and other characters


(such as {, @, #, etc.) defined in the ASCII character set.
The characters are internally stored as integers that range in value from -128
to 127. The char data type occupies 1 byte of memory (that is, it holds only one
character at a time).

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 6
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

b. Int: Numbers without the fractional part represent integer data. In C++, the int
data type is used to store integers such as 4, 42, 5233, -32, -745. The int data type
occupies 2 byte of memory

ii. Floating-point Data Type: A floating-point data type is used to store real numbers
such as 3 .28, 64. 755765, 8.01, -24.53. This data type includes float and double data
types. The float data type occupies 4 byte of memory , the double data type
occupies 8 byte of memory.

iii. Void:
● The void data type is used for specifying an empty parameter list to a
function and return type for a function.
● When void is used to specify an empty parameter list, it indicates that a function
does not take any arguments and when it is used as a return type for a
function, it indicates that a function does not return any value.
● For void, no memory is allocated and hence, it cannot store anything.
● As a result, void cannot be used to declare simple variables, however, it can
be used to declare generic pointers.

iv. Bool and wcha_t :


● The bool data type can hold only Boolean values, that is; either true or false,
where true represents 1 and false represents O.
● It requires only one bit of storage, however, it is stored as an integer in the
memory. Thus, it is also considered as an integral data type.
● The bool data type is most commonly used for expressing the results of logical
operations performed on the data.
● It is also used as a return type of a function indicating the success or the
failure of the function.
● wchar_t which is used to store 16- bit wide characters. Wide characters are
used to hold large character sets associated with some non-English languages.

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 7
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

ii) Derived Data Types:

Data types that are derived from the built-in data types are known as derived
data types. The various derived data types provided by C++ are arrays, junctions,
references and pointers.

i. Array

An array is a set of elements of the same data type that are referred to by
the same name. All the elements in an array are stored at contiguous (one after
another) memory locations and each element is accessed by a unique index or
subscript value. The subscript value indicates the position of an element in an array.

ii. Function A function is a self-contained program segment that carries out a


specific well-defined task. In C++, every program contains one or more functions
which can be invoked from other parts of a program, if required.

iii. Reference A reference is an alternative name for a variable. That is, a reference
is an alias for a variable in a program. A variable and its reference can be used
interchangeably in a program as both refer to the same memory location. Hence,
changes made to any of them (say, a variable) are reflected in the other (on a
reference).

iv. Pointer A pointer is a variable that can store the memory address of another
variable. Pointers allow to use the memory dynamically. That is, with the help of
pointers, memory can be allocated or de-allocated to the variables at run-time, thus,
making a program more efficient.

iii) User-Defined Data Types

Various user-defined data types provided by C++ are structures, unions,


enumerations and classes.

i. Structure, Union and Class: Structure and union are the significant features of C
language. Structure and union provide a way to group similar or dissimilar data
types referred to by a single name. However, C++ has extended the concept of

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 8
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

structure and union by incorporating some new features in these data types to support
object -oriented programming.

C++ offers a new user-defined data type known as class, which forms the basis of
object-oriented programming. A class acts as a template which defines the data
and functions that are included in an object of a class. Classes are declared using
the keyword class. Once a class has been declared, its object can be easily created.

ii. Enumeration: An enumeration is a set of named integer constants that specify


all the permissible values that can be assigned to enumeration variables. These
set of permissible values are known as enumerators.

2.1.1.3 Type Structure in Java

Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float and double.

2. Non-primitive data types: The non-primitive data types include Classes,


Interfaces, and Arrays.

Data Type Default Value Default size

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 9
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

a. Boolean

The Boolean data type is used to store only two possible values: true and false. This data
type is used for simple flags that track true/false conditions.

The Boolean data type specifies one bit of information, but its "size" can't be defined
precisely.

Example:

boolean one = false

b. Byte Data Type

It is an 8-bit signed two's complement integer. Its value-range lies between


-128 to 127 (inclusive). Its default value is 0.
The byte data type is used to save memory in large arrays where the memory
savings is most required.

Example:

byte a = 10, byte b = -20

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 10
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

c. Short Data Type

The short data type is a 16-bit signed two's complement integer. Its value-range
lies between -32,768 to 32,767 (inclusive).

Example:

short s = 10000, short r = -5000

d. Int Data Type

The int data type is a 32-bit signed two's complement integer. Its value-range lies
between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).

Example:

int a = 100000, int b = -200000


e. Long Data Type

The long data type is a 64-bit two's complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)
(inclusive).

Example:

long a = 100000L, long b = -200000L

f. Float Data Type

The float data type is a single-precision 32-bit IEEE 754 floating point. Its value
range is 3.4028 *1038 - 1.40239* 10-45
The float data type should never be used for precise values, such as currency. Its
default value is 0.0F.
Example:

float f1 = 234.5f

g, double Data Type

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 11
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

The double data type is a double-precision 64-bit IEEE 754 floating point.

Example:

double d1 = 12.3

h. Char Data Type

The char data type is a single 16-bit Unicode character. Its value-range lies between '\
u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store
characters.

Example:

char letterA = 'A'

2.1.2 Character String types

● Character string type is one in which the values consist of sequences of


characters
● Design issues with the string types

○ Should strings be simply a special kind of character array or a


primitive type?
■ C and C++
● not primitive
● use char arrays and a library of functions that
provide operations
■ Java : String class (not arrays of char)
● objects are immutable

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 12
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

● StringBuffer is a class for changeable string objects


● String length options
○ Should strings have static or dynamic length?

■ Length can be fixed or changing

■ In C, C++, Java we have static length string whereas in Perl,


JavaScript we have dynamic length string

2.1.2.1 String Operations

2.1.2.2 String Length

● Static – Python, Java’s String class, C++ standard class library, Ruby’s built-in
String class, and the .NET class library in C# and F#.
● Limited dynamic length – C and C++ ( up to a max length indicated by a null
character)

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 13
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

● Dynamic –Perl, JavaScript

2.1.2.3 Character String Type Implementation

● Static length - Compile-time descriptor is required with three fields

● Limited Dynamic - may need a run-time descriptor for storing length (but
not in C and C++ because the end of a string is marked with the null character)

● Dynamic - need complex storage management. Length of string is not fixed ,


hence storage grows or shrinks dynamically

● First approach :

○ Using linked list

○ Disadvantage - Extra storage for links and complexity of

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 14
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

operations
● Second approach :

○ Store as array of pointers to individual characters allocated in a


heap.
○ Disadvantage- Still uses extra memory

● Third approach:
○ To store complete strings in adjacent storage cells

○ When a string grows and adjacent storage is not available, a new area
of memory is found that can store the complete new string and the
old part is moved to this area, and the memory cells for the old
string are deallocated.
○ This results in faster string operations and requires less storage

○ Disadvantage : = Allocation / deallocation process is slower.

2.1.3 User Defined Ordinal Types

An ordinal type is one in which the range of possible values can be easily associated
with the set of positive integers

1) Enumeration Type
2) Subrange

1) Enumeration
● All possible values, which are named constants, are provided in the
definition
● C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
● The enumeration constants are typically implicitly assigned the integer
values, 0, 1, …, but can be explicitly assigned any integer literal in the
type’s definition
● In languages that do not have enumeration types, programmers

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 15
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

usually simulate them with integer values.


E.g. Fortran 77, use 0 to represent blue and 1 to represent red:
INTEGER RED, BLUE
DATA RED, BLUE/0,1/

● Example

○ C++
➔ enum colors {red, blue, green, yellow, black};
➔ colors myColor = blue, yourColor = red;
➔ The enumeration values are coerced to int when they are
put in integer context.
➔ E.g. myColor++ would assign green to myColor.

○ Java
■ All enumeration types are implicitly subclasses of the
predefined class Enum. They can have instance data
fields, constructors and methods.
■ Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Monday");

dayNames.add("Friday");
days = dayNames.elements();
while (days.hasMoreElements())
System.out.println(days.nextElement());
○ C# - Enumeration types are like those of C++ except that they
are never coerced to integer.

Design Issues for Enumeration Types?


a) Is an enumeration constant allowed to appear in more than one type
definition, and if so, how is the type of an occurrence of that constant
checked?
b) Are enumeration values coerced to integer?
c) Any other type coerced to an enumeration type?

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 16
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

Problem:

There is no type checking when they are used

Features
● Aid to readability, e.g., no need to code a color enum type as a number
● Aid to reliability, e.g., compiler can check: operations (don’t allow color enum
types to be added)
● No enumeration variable can be assigned a value outside its defined range,
○ e.g. if the colors type has 10 enumeration constants and uses 0 .. 9 as
its internal values, no number greater than 9 can be assigned to a
colors type variable.
● Ada, C#, and Java 5.0 provide better support for enumeration than C++
because enumeration type variables in these languages are not coerced
into integer types

2) Subrange
● An ordered contiguous subsequence of an ordinal type
● Not a new type, but a restricted existing 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;
Features of SubRange Type
● Aid to readability
○ Make it clear to the readers that variables of subrange can store
only certain range of values

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 17
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

● Reliability
○ Assigning a value to a subrange variable that is outside the
specified range is detected as an error.

2.1.4 Array types


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.

2.1.4.1 Indexes in Arrays

● Indexing (or subscripting) is a mapping from indices to element

○ array_name (index_value_list) -> element

● Index Syntax

○ FORTRAN, PL/I, Ada use parentheses , Sum-:= Sum +B(I);

○ Ada explicitly uses parentheses to show uniformity between array


references and function calls because both are mappings
○ Most other languages use brackets

○ E.g. List(27) direct reference to the List array’s element with the subscript 27.

Language Index Type

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 18
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

FORTRAN , C, Java Integer

PASCAL Any Ordinal Type


(Integer, Boolean,
Enumeration,
Character)

Ada Integer Or
Enumeration

● C, C++, Perl, and Fortran do not specify range checking

● Java, ML, C# specify range checking

2.1.4.2 Design Issues of Arrays

● What types are legal for subscripts?

● Are subscripting expressions in element references range checked?

● When are subscript ranges bound?

● When does allocation take place?

● Are ragged or rectangular multi dimensioned arrays allowed, or both?

● Can arrays be initialized when they have their storage allocated?

● Are any kind of slices allowed?

2.1.4.3 Subscript Bindings


● Binding is usually static

● Subscript value ranges are dynamically bound

● Lower bound are implicit

● In C based languages- lower bound of all index ranges is fixed at 0;

● Fortran 95 defaults to 1

● In some other languages, ranges must be completely specified by the

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 19
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

programmer

2.1.4.4 Array occurs in 5 categories based on Subscript bindings

i) Static: subscript ranges are statically bound and storage allocation is static
(before run‐ time)
● Advantage: efficiency (no dynamic allocation/deallocation required)

● Example: In C and C++ arrays that include the static modifier are static

○ static int myarray[3] = {2, 3, 4};

ii) Fixed-Stack Dynamic :

● Subscript ranges are statically bound, but the allocation is done at


declaration time
● Advantage: space efficiency

● Example: arrays without static modifier are fixed stack‐dynamic

○ int array[3] = {2, 3, 4};

○ E.g. void foo()

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 20
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

{
int fixed_stack_dynamic_array[7];
/* ... */
}

iii) Stack - Dynamic Array

● Stack‐dynamic: subscript ranges are dynamically bound and the storage


allocation is dynamic (done at run‐ time)
● Advantage: flexibility (the size of an array need not be known until the array is
to be used)
● Ada Arrays
void foo(int n)
{
int stack_dynamic_array[n];
/* ... */
}

iv) Fixed Heap Dynamic Array :

● Similar to fixed stack‐ dynamic: storage binding and subscript binding is


dynamic but fixed after allocation (i.e., binding is done when requested &
storage is allocated from heap, not stack)
● Example: In C/C++, using malloc/new to free/delete memory from the heap

● Java has fixed heap dynamic arrays

● C# includes a second array class ArrayList that provides fixed heap‐dynamic


int * fixed_heap_dynamic_array = malloc(7 * sizeof(int));

v) Heap Dynamic Array ;

● Binding of subscript ranges and storage allocation is dynamic and can change
any number of times
● Advantage: flexibility (arrays can grow or shrink during program execution)
● Examples: Perl, JavaScript, Python, and Ruby support heap‐dynamic arrays

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 21
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

● Perl: @states = (“Idaho",“Washington",“Oregon");


● Python: a = [1.25, 233, 3.141519, 0, ‐1]

void foo(int n) {
int * heap_dynamic_array = malloc(n * sizeof(int));
}

Static int static_array[7];

fixed stack‐dynamic void foo()


{ int fixed_stack_dynamic_array[7]; }

Stack‐dynamic void foo(int n)


{ int stack_dynamic_array[n];}

fixed heap_dynamic_array int * fixed_heap_dynamic_array =


malloc(7 * sizeof(int));

void foo(int n)
heap_dynamic_array { int * heap_dynamic_array = malloc(n *
sizeof(int)); }

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 22
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

2.1.4.5 Array Initialization

Some language allow initialization at the time of storage allocation


● Fortran
Integer, Dimension (3) :: List =( /0, 5, 5/) // List is initialized to the values
● C, C++, Java, C# example
int list [] = {4, 5, 7, 83}
● Character strings in C and C++
char name [] = “freddie”; // eight elements, including last element as null character
● Arrays of strings in C and C++
char *names [] = {“Bob”, “Jake”, “Joe”];
● Java initialization of String objects
String[] names = {“Bob”, “Jake”, “Joe”};
● Ada positions for the values can be specified:
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 KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 23
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

2.1.4.6 Array Operations

Language Array Operation

C-based No Operations , only through methods

C#, Perl Array Assignments

Ada Assignment, Concatenation(&), Comparison

Concatenation(+), Element Membership(in),


Python
Comparison(==, is)

FORTRAN Matrix Multiplication, Transpose

APL(Most powerful) +, +.* (First multiply then Add)

2.1.4.7 Rectangular Arrays and Jagged Arrays


Rectangular Arrays
A rectangular array is a multidimensioned array in which all of the rows have the same
number of elements and all of the columns have the same number of elements.
e.g. int a[3][4]

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 24
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

JAGGED ARRAY

A jagged array is one in which the lengths of the rows need not be the same. For

example, a jagged matrix may consist of three rows, one with 5 elements, one with 7

elements, and one with 12 elements. This also applies to the columns and higher

dimensions.

2.1.4.8 Slices

● A slice is some substructure of an array; nothing more than a referencing


mechanism
● Slices are only useful in languages that have array operations

● The Operator : (colon) is used to indicate the slice

● For eq. Fortran Declaration:

○ Vector(3:6) Four element from third to sixth

○ Python Declaration

■ A =[10,20,30,40,50,60]

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 25
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

■ A[1:4] [10,20,30]

■ A[:5] [10,20,30,40,50]

■ A[4:] [50,60]

2.1.5 Associative Arrays

● An associative array is an unordered collection of data elements that are


indexed by an equal number of values called keys
● Each element here has a pair of key and value.

● Also known as Hash tables

● Index by key (part of data) rather than value

● Store both key and value (take more space)

● Best when access is by data rather than index

● Example :

○ In Perl

%lookup =

("dave", 1234,
“peter", 3456,
"andrew", 6789);

● The reference a particular value you do: $lookup{"dave"}


● New elements by assignments to new keys
○ $lookup{"adam"} = 3845
● new assignments to old keys also
○ # change dave's code $lookup{"dave"} = 7634;

2.1.6 Record Types

A record is a collection of fields, possibly of different data types. Every field has
an identifier (field name) and a data type.

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 26
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

Implementation in Different Programming Languages:

i. Records in C, C++, C# :
In C, C++, and C#, records are supported with the struct data type.

e.g.
Defining Linked List:
struct list
{
int info;
struct list *ptr;
};

Defining Linked List variable

struct list node;

ii. COBOL uses level numbers to show nested records; others use recursive definition

01 EMP-REC.

02 EMP-NAME.

05 FIRST PIC X(20).

05 MID PIC X(10).

05 LAST PIC X(20).

02 HOURLY-RATE PIC 99V99.

iii. ADA
type Emp_Rec_Type is record

First: String (1..20);


Mid: String (1..10);
Last: String (1..20);
Hourly_Rate: Float;

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 27
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

end record;
Emp_Rec: Emp_Rec_Type;

Access to Record Fields : Most Languages Use dot operator to access the fields of
record.

2.1.7 Union

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 28
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

https://www.comrevo.com/2021/04/record-types-ppl-sebesta-data-types-in-ppl.html

2.1.7 Union Types

2.1.8 Pointer and reference Type.

Blooms
Questions Marks
Level

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 29
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program

SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 30
Principles of Programming Languages

You might also like