1 of 71

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

2 of 71

Unit 2

Structuring the Data, Computations and Program

  • 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 Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

3 of 71

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

4 of 71

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

5 of 71

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

6 of 71

Primitive Data Types

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

7 of 71

Primitive Data Types

  • Primitive data types are those that are not defined in terms of other data types

  • Early PLs had only numeric primitive types, and still play a central role among the collections of types supported by contemporary languages.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

8 of 71

Numerical Data Types …Integer

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

9 of 71

Numerical Data Types …Floating Point

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

10 of 71

Numerical Data Types …Floating Point

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

11 of 71

Numerical Data Types …Decimal

  • Advantage
    • Accuracy
  • Disadvantages:
    • Limited range
    • Wastes memory

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

12 of 71

Boolean Types

Advantage: � Readability

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

13 of 71

Character Types

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

14 of 71

Character String Types

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

15 of 71

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

16 of 71

Character String Types.. String Length Options

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

17 of 71

Character String Types

  • 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
    • StringBuffer is a class for changeable string objects
    • String length options
  • 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)
  • Dynamic –Perl, JavaScript

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

18 of 71

Used Defined Ordinal Types

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

19 of 71

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

20 of 71

Enumeration Types

  • 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

Design issues

  • 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?
  • Are enumeration values coerced to integer?
  • Any other type coerced to an enumeration type?

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

21 of 71

Subrange Type

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;

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

22 of 71

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

23 of 71

Array and Indexes

  • 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.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

24 of 71

Array and Indexes

Language

Index Type

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

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

25 of 71

Categories of Arrays

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

26 of 71

Categories of Arrays

  • 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};
  • int static_array[7];

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

27 of 71

Array Initialization

Some language allow initialization at the time of storage allocation

  • Fortran

List (3) Data 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”];

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

28 of 71

Array Initialization

  • Some language allow initialization at the time of storage allocation
  • 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)

  • Concatenation. One array can be appended to another array using the & operator provided that they are of the same type.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

29 of 71

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

30 of 71

Associative Array

  • An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys
  • 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

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

31 of 71

Associative Array

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

32 of 71

Record Type

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

33 of 71

Record Type

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

34 of 71

Union

  • A union is a type whose variables are allowed to store different type values at different times during execution
  • Design issues
    • Should type checking be required?
    • Should unions be embedded in records?

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

35 of 71

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

36 of 71

Pointer and reference type

  • A pointer type variable has a range of values that consists of memory addresses and a special value, nil
  • Provide the power of indirect addressing
  • Provide a way to manage dynamic memory
  • A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap)

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

37 of 71

Pointer Operations

  • Two fundamental operations: assignment and dereferencing
  • Assignment is used to set a pointer variable’s value to some useful address
  • Dereferencing yields the value stored at the location represented by the pointer’s value
  • Dereferencing can be explicit or implicit
  • C++ uses an explicit operation via *

j = *ptr

sets j to the value located at ptr

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

38 of 71

Pointer Assignment Illustrated

The assignment operation

j = *ptr

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

39 of 71

  • Expressions are the fundamental means of specifying computations in a programming language.
  • To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation.
  • The purpose of assignment variable is to change the value of variable.

39

Introduction to Expression

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

40 of 71

  • Arithmetic evaluation was one of the motivations for the development of the first programming languages.

  • Arithmetic expressions consist of operators, operands, parentheses, and function calls.

  • Operators can be:

    • A unary operator has one operand.

    • A binary operator has two operands.

    • A ternary operator has three operands.

40

Arithmetic Expression

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

41 of 71

Unary Arithmetic Operators in C/C++ and Java

  • Unary Plus ( +A ):

    • Examples: +num, +num/5, num1 * +num2

    • Has no effect on its operand in C/C++.

    • In Java, it causes an implicit conversion of a char, short, or byte operand to int type; otherwise, it has no effect on its operand.

  • Unary Minus ( -A ):

    • Examples: -num, -num/5, num1 * -num2

41

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

42 of 71

Unary Arithmetic Operators in C/C++ and Java (cont.)

  • The pre-increment is specified as follows: (++a)

    • It says to add 1 to the current value of the variable and then to use the new value (if necessary).

  • The post-increment is specified as follows: (a++)

    • It says to use the current value of the variable (if necessary) and then add 1 to it.

  • The pre-decrement is specified as follows: (--a)

    • It says to subtract 1 from the current value of the variable and then to use the new value (if necessary).

  • The post-decrement is specified as follows: (a--)

    • It says to use the current value of the variable (if necessary) and then subtract 1 from it.

42

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

43 of 71

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

44 of 71

Ternary Operator in C/C++ and Java

A ternary operator ?: ” has three operands.

average = (a>b)? a : b;

    • Evaluates as if written like:

if (a>b) average = a;

else average = b;

44

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

45 of 71

Arithmetic Expressions :Conditional Expressions

  • Conditional Expressions:

    • C-based languages (e.g., C, C++).

    • An example:

average = (a>b)? a : b;

    • Evaluates as if written like:

if (a>b) average = a;

else average = b;

45

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

46 of 71

Overloaded Operators

  • Operator Overloading is accomplished by defining functions that have the same name as the operator being overloaded.

  • Some are common (e.g., +, -, *, … for int and float).

  • C++, FORTRAN 95 and Ada allow user-defined overloaded operators.

  • Java does not permit operator overloading.

46

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

47 of 71

Overloaded Operators - division operator issue

  • avg= sum/count
  • Avg is floating point but sum and count are integer
  • The result gives integer value for Avg

47

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

48 of 71

Explicit Type Conversions

  • Explicit Type Conversions:

    • Called casting in C-based language.

    • Examples:

      • C conversion (cast)

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

49 of 71

Relational and Boolean Expressions

  • Relational operators in various languages:
    • Ada: =, /=, >, <, >=, <=
    • Java: ==, !=, >, <, >=, <=
  • Relational operators always have lower precedence than arithmetic operators.
  • Relational Expressions:
    • Use relational operators and operands of various types.
    • Evaluate to some Boolean representation.
  • Boolean Expressions:
    • Operands are Boolean and the result is Boolean.
    • C/C++ operators: &&, ||, !
    • C has no Boolean type, it uses int type with 0 for false and nonzero for true.

49

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

50 of 71

Short-Circuit Evaluation

  • A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators.

  • Types:

    • In Arithmetic Expressions:

      • Ex.: (13*a) * (b/13–1)

If a is zero, there is no need to evaluate (b/13-1).

    • In logical Expressions:

      • Ex.: The value of (a >= 0) and (b < 10) is independent of the second relational expression if a < 0.

50

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

51 of 71

Assignment Statements

  • The general syntax:

<target_var> <assign_operator> <expression>

  • The assignment operator:

= FORTRAN, BASIC, PL/I, C, C++, Java.

:= ALGOLs, Pascal, Ada.

  • Simple assignment:
    • a = b;
    • a = b = c;
      • Suppose a, b, and c are integers.
      • In C, the integer value of c is assigned to b, which is in turn assigned to a (multiple targets).

51

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

52 of 71

Assignment statements (cont.)

  • Unary assignment operators:
    • count++;
    • ++count;
    • sum = ++count; 🡺 count incremented then assigned to sum
    • sum = count++; 🡺 count assigned to sum then incremented

  • Assignment as an expression:

    • In C, C++, and Java the assignment statement produces a result and can be used as operands.

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

53 of 71

Selection Statements

  • A selection statement provides the means of choosing between two or more paths of execution
  • Two general categories:
    • Two-way selectors
    • Multiple-way selectors

1-53

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

54 of 71

Iterative Statements

  • The repeated execution of a statement or compound statement is accomplished either by iteration or recursion
  • General design issues for iteration control 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

55 of 71

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

56 of 71

Fundamentals of Subprograms

1-56

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

57 of 71

Fundamentals of Subprograms

  • Each subprogram has a single entry point
  • The calling program is suspended during execution of the called subprogram
  • Control always returns to the caller when the called subprogram’s execution terminates

1-57

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

58 of 71

Basic Definitions

  • A subprogram definition describes the interface to and the actions of the subprogram abstraction
  • A subprogram call is an explicit request that the subprogram be executed
  • A subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters
  • The parameter profile (aka signature) of a subprogram is the number, order, and types of its parameters
  • The protocol is a subprogram’s parameter profile and, if it is a function, its return type

1-58

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

59 of 71

Basic Definitions

1-59

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

60 of 71

1-60

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

61 of 71

Design Issues for Subprograms

  • What parameter passing methods are provided?
  • Are parameter types checked?
  • Are local variables static or dynamic?
  • Can subprogram definitions appear in other subprogram definitions?
  • Can subprograms be overloaded?
  • Can subprogram be generic?

1-61

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

62 of 71

Local Referencing Environments

  • Variables that are defined inside subprograms are called local variables.
  • Local variables can be either static or stack dynamic
  • “bound to storage when the program begins execution and are unbound when execution terminates.”

1-62

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

63 of 71

Parameter Passing Methods

  • Ways in which parameters are transmitted to and/or from called subprograms
    • Pass-by-value
    • Pass-by-result
    • Pass-by-value-result
    • Pass-by-reference
    • Pass-by-name

1-63

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

64 of 71

Abstract Data Type and Encapsulation Constructs

  • Design issues for Abstraction,
  • Parameterized Abstract Data types,
  • Encapsulation Constructs,
  • Naming Encapsulations

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

65 of 71

The Concept of Abstraction

  • An abstraction is a view or representation of an entity that includes only the most significant attributes
  • The concept of abstraction is fundamental in programming (and computer science)
  • Nearly all programming languages support process abstraction with subprograms
  • Nearly all programming languages designed since 1980 support data abstraction

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

66 of 71

Introduction to Data Abstraction

  • An abstract data type is an enclosure that includes only the
    • data representation of one specific data type and
    • the subprograms that provide the operations for that type
  • An instance of an abstract data type is called an object.

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

67 of 71

Design Issues for Abstract Data Types

  • What is the form of the container for the interface to the type?
  • Can abstract types be parameterized?
  • What access controls are provided?

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

68 of 71

Parameterized Abstract Data Types

  • Parameterized ADTs allow designing an ADT that can store any type elements (among other things) – only an issue for static typed languages
  • Also known as generic classes
  • C++, Ada, Java 5.0, and C# 2005 provide support for parameterized ADTs

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

69 of 71

Encapsulation Constructs

  • Large programs have two special needs:
    • Some means of organization, other than simply division into subprograms
    • Some means of partial compilation (compilation units that are smaller than the whole program)
  • Obvious solution: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) Such collections are called encapsulation

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

70 of 71

Naming Encapsulations

  • Large programs define many global names need a way to divide into logical groupings
  • A naming encapsulation is used to create a new scope for names

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering

71 of 71

SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering