1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Programming Languages and Paradigms
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Names and Bindings.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
1) Causes of errors 2) Classification of errors 3) Signals and exceptions 1) Program hierarchy 2) Blocks 3) Routines 4) Procedures and functions COMP205.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
CENG 311 Machine Representation/Numbers
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
Input & Output: Console
Names Variables Type Checking Strong Typing Type Compatibility 1.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CPS120: Introduction to Computer Science
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CPS120: Introduction to Computer Science Variables and Constants.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Bill Tucker Austin Community College COSC 1315
A variable is a name for a value stored in memory.
The Machine Model Memory
Variables Mr. Crone.
Introduction to the C Language
COMP205 IMPERATIVE LANGUAGES
Revision Lecture
Variables and Primative Types
Introduction to the C Language
Chapter 11 Introduction to Programming in C
2.1 Parts of a C++ Program.
College of Computer Science and Engineering
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables Title slide variables.
Expressions and Assignment
Chapter 2: Introduction to C++.
Chapter 7 Expressions and Assignment Statements.
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
PRESENTED BY ADNAN M. UZAIR NOMAN
Variables and Constants
Chapter 12 Variables and Operators
Presentation transcript:

1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE LANGUAGES 2. DATA

DATA - "that which is given" A data item has a number of attributes: 1) An Address 2) A Value which in turn has: a) An Internal Representation comprising one or more bits and b) An interpretation of that representation. 3) A set of Operations that may be performed on it. 4) A Name to tie the above together (also referred to as a label or identifier). The nature of these attributes is defined by the type of the data item.

ADDRESS The address (reference) of a data item is the physical location in memory (computer store) of that data item. The amount of memory required by a particular data item is dictated by its type, e.g. integer or character.

VALUE The binary number stored at an address associated with a data item is its value. The required interpretation is dictated by the type of the integer. Consider: This might be interpreted as: The Decimal integer `90’ The Hexadecimal integer `5A’ The Octal integer `132’ The ASCII character (capital) `Z’

OPERATIONS The type of a data item also dictates the operations that can be performed on it. For example numeric data types can have the standard arithmetical operations performed on them while string data types cannot.

NAMES To do anything useful with a data item all the above must be tied together by giving each data item an identifying name (also referred to as a label or identifier): Names are usually made up of alphanumeric characters (and should be descriptive). White space is usually not allowed (exception Algol 60). Some languages (Ada and C included) allow underscores. Some languages restrict the number of characters. Some allow any length but treat the first N characters as significant (first 31 in ANSI C). Case may be significant: in Algol'60, C and Modula 2 case matters, in Pascal and Ada it does not. Cannot use key/reserved words as names. Naming conventions.

SUMMARY Data Items comprise (a) a name, (b) an address and (c) a value: (Barron 1977) It is important to distinguish beween these. The type of a data item dictates: a) The amount of storage required for its internal representation. b) The interpretation of the internal representation. c) The operations that can be performed on the item.

( Bal & Grune, 1994)

DATA DECLARATIONS, ASSIGNMENT AND INITIALISATION A data declaration introduces a data item. This is typically achieved by associating the item (identified by a name) with a data type. NUMBER : integer; number : integer; int number; Declaration examples (right) in Ada, Pascal and C. Assignment is the process of associating a value with a data item. Usually achieved using an assignment operator.

Assignment examples in Ada, Pascal and C: NUMBER := 2; number := 2; number = 2; NUMBER : integer:= 2; int number = 2; Initialisation is then the process of assigning an "initial" value to a data item upon declaration (not all imperative languages support this, C and Ada do, Pascal does not). Initialisation examples in Ada and C:

ORDERING OF DECLARATIONS In some imperative languages (notably Pascal) declarations must be presented in a certain order, e.g. constants before variables. NUMBER1, NUMBER2 := 2; Many imperative languages (Ada and Pascal, but not C) support the concept of multiple assignment where a number of variables can be assigned a value (or values) using a single assignment statement. Ada example: MULTIPLE ASSIGNMENTS

In Pascal we do not have to assign the same value to each variable when using a multiple assignment statement: In Pascal we can also write: Which has the effect of "doing a swap". number1, number2 := 2, 4; number1, number2 := number2, number1;

POSITIONING OF DECLARATIONS In languages such as C, Ada and Pascal data is declared at the beginning of a function or procedure (other than C global data items). Some languages allow declarations to occur anywhere within a procedure or function (for example the object oriented languages C++ and Java). Whatever the case remember that: A data item cannot be used until it has been declared. It is good practice to adopt some sensible and systematic procedure for declaring data items, e.g. (if the language supports this) immediately before they are used.

GLOBAL AND LOCAL DATA Data items have associated with them: a) A life time - the time-span during the running of a program when they can be said to exist. b) A visibility - the parts of the program from where they can be accessed ("seen"). The nature of the life time and visibility of a data item is dictated by what are referred to as scoping rules. In this respect there are two types of data: Global data Local data

VARIABLES Given that we can always replace a particular bit pattern with another, the value of a data item can always be changed. Imperative languages support such change (destructive assignment). Data items that are intended to be used in this way are referred to as variables. In most imperative languages (including Ada and C) data items are assumed to be variables by default (in Pascal we must include the key word var in the declaration).

UNINITIALISED VARIABLES It is possible to declare a data item that has no value (other than an arbitrary bit pattern). Such a data item is referred to as an uninitialised variable. Uninitialised variables are dangerous. Individual imperative languages treat the phenomena of uninitialised variables differently. Common approaches include: a) Ignore the problem and leave it up to the programmer not to use them. b) Consider their usage to represent an error. c) Allocate an appropriate value to such variables.

CONSTANTS It is sometimes desirable to define a data item whose value cannot be changed. Such data items are referred to as constants and are usually indicated by incorporating a predefined key word into the declaration. Examples in Ada & C: NUMBER: constant := 2; const int number = 2; What we are doing here is telling the Compiler to "flag" the data item as a constant (Software Protection). Theoretically we can still change the bit pattern representing the value.

In Pascal we declare constants by grouping then together in a const section (followed by the var section): Note: user-defined type declarations (if required) are included in a type section located after the const section and before the var section. const label = 2; pi = ; var number integer;

#include int GLOBAL_VAR = 0; const int GLOBAL_CONST = 1; /* Main function */ int main(void) { int localVar = 2; const int localConst = 3; printf("%d, %d, %f, %d \n",GLOBAL_VAR,GLOBAL_CONST, localVar,localConst); GLOBAL_VAR = GLOBAL_CONST + (localVar*localConst); printf("%d, %d, %f, %d \n",GLOBAL_VAR,GLOBAL_CONST, localVar,localConst); }

with TEXT_IO; use TEXT_IO; procedure TOP_LEVEL is package INT_INOUT is new INTEGER_IO(integer); use INT_INOUT; GLOBAL_VAR : integer := 0; GLOBAL_CONST : constant := 1; SECOND LEVEL PROCEDURE procedure PROC_1 is LOCAL_VAR : integer := 2; LOCAL_CONST : constant := 3; begin Output and addition in here end PROC_1; TOP LEVEL begin PROC_1; end TOP_LEVEL;

begin put(GLOBAL_VAR); put(", "); put(GLOBAL_CONST); put(", "); put(LOCAL_VAR); put(", "); put(LOCAL_CONST); new_line; -- Sum GLOBAL_VAR := GLOBAL_CONST + (LOCAL_VAR*LOCAL_CONST); -- Output put(GLOBAL_VAR); put(", "); put(GLOBAL_CONST); put(", "); put(LOCAL_VAR); put(", "); put(LOCAL_CONST);new_line; end PROC_1;

program myProg(Output); {Example program} const globalConst = 1; var globalVar : integer; procedure proc1; {second level procedure} const localConst = 3; var localVar : integer; begin {Proc_1} Output and addition in here end; {Proc1} begin {myProg} globalVar := 1; proc1; end. {myProg}

begin {Proc_1} write(globalVar); write(', ',globalConst); write(', ',localVar); writeLn(', ',localConst); {Sum} globalVar := globalConst + (localVar*localConst); {Output} write(globalVar); write(', ',globalConst); write(', ',localVar); writeLn(', ',localConst); end; {Proc1}

1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs SUMMARY