Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Types and Arithmetic Operators
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Elementary Data Types Scalar Data Types Numerical Data Types Other
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
String Escape Sequences
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Imperative Programming
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Copyright Curt Hill Variables What are they? Why do we need them?
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Syntax (2).
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CPS120: Introduction to Computer Science Variables and Constants.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Definition of the Programming Language CPRL
The Machine Model Memory
Chapter 2.
High Level Programming Languages
Expressions and Assignment
Data Types and Expressions
The Data Element.
Chap 2. Identifiers, Keywords, and Types
The Data Element.
Presentation transcript:

Imperative Programming Part One

2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are not in Clite.

3 Imperative Languages Imperative languages are characterized by 3 properties:  the sequential execution of instructions,  the use of variables representing memory locations, and  the use of assignment to change the values of variables Its primary feature is a sequence of statements representing commands (imperatives).

4 Example

5 Imperative Languages A programming language is said to be Turing complete if it  contains integer variables, values, and operations  has assignment statements  has control constructs of statement sequencing, conditionals and branching statements. An imperative programming language is one which is Turing complete and supports the following features:  Data types for real number, characters, string, boolean etc;  Control structures, for and while loops, case (switch) statements;  Arrays and element assignment;

6 Naming and Variables Names or identifiers in Clite  denote locations in the memory  composed of letters and digits; the first character must be a letter Recall: [a-z A-Z] [a-z A-Z 0-9]*  no restriction on number of characters (must fit in one line)  case-insensitive; fib, Fib, and FIB are all the same.

7 Naming and Variables (2) Some names in Clite are reserved:  they may not be used for variable names  e.g. int, boolean, while, if, else, … etc. Modern languages try to limit their reserved words.

8 Naming and Variables (3) In some languages, some names are merely predefined.  programmers are free to redefine them  such feature minimizes the number of reserved words,  but also may lead to confusion! var true: boolean; begin true := false; … end

9 Naming and Variables (4) In Clite, each location in the memory is  declared with a unique name  associated with type  assigned with value A valid Clite program cannot declare two different variables with the same name V(Declarations d) =  i, j  {1,…, n}:(i  j  d i.v  d j.v)

10 Elementary types & values A Clite type is defined as a set of values and a set of operations on those values. The basic types in Clite are: int, float and boolean. type Int and floatboolean values{…, -1, 0, 1, …}{true, false} operations arithmetic +, , *, / &&, ||, ! relational ==, !=, ,  =, ,  =

Elementary types in real languages Data types in programming languages can be partitioned into two classes:  elementary types: denote data values that can be stored in fixed memory space.  structured types: require a variable memory space to store their values.

Elementary types in C++, Ada, & Java TypeC/C++AdaJava Byte byte Integer short, int, long integershort, int, long Real number float, double, long double float, decimal float, double Character charcharacterchar Boolean boolboolean Pointer *access

Memory units Memory requirements for the above different types are based on the following memory units:  Nibble: 4 bits  Byte: 8 bits = 1 byte byte  Half-word: 16 bits = 2 bytes short  Word: 32 bits = 4 bytes int, float  Double word: 64 bits = 8 bytes long, double  Quad word: 128 bits = 16 bytes

Strings The language C does not recognize character strings as a distinct data type.  strings are encoded as arrays of ASCII characters  one character per byte  terminated by a null character (all bits zero)  E.g. The string “hello” would be encoded as: c 6c 6f 00 …  If the size of the array is greater than the size of the string + 1 then the remaining elements of the array are undefined

Strings (2) In the language Java, String is a language- defined object type  its exact representation is hidden from the programmer  stored in Unicode using 2 bytes per character  E.g. The string “hello” would be encoded as: c 00 6c 00 6f  the left-hand byte of every ASCII character in Unicode is zero (hex 00)

Expressions in real languages In actual programming languages, there is a richer variety of operators, and hence a richer variety of expressions. Refer to the textbook for a list of available operators in C, C++ and Java.

Conditional operator :? C, C++ and Java use unusual operator :? It embeds a complete “if…then…else”! General form: ? : E.g. c = (a > b) ? a : b; is equivalent to: if (a > b) c = a; else c = b;

Operator overloading, conversion, & casting C, C++, and Java support overloading their operators. An operator is overloaded if it can be used  in different context  with different types of operands and  compute different results

Operator overloading, conversion, & casting (2) Consider, k = i / j;  In Clite, this expression always carry the same meaning ( int, float ).  In C/C++/Java, the outcome depends on the types of i, j, and k, and possible intermediate conversions A conversion (or casting) translates data values form one type to another. E.g. int value 1 is equivalent to float value 1.0

Operator overloading, conversion, & casting (3) Explicit conversion can be specified using the cast operator. k is int, i and j are double: k = (int) i/j Here, we are overriding the warning that information may be lost and saying it’s ok to truncate the double quotient.

Syntax and Semantics of Statements in Real Languages Imperative languages offer wider variety of statement types. Common other statements includes:  other forms of loop statements  other forms of conditional statements  …

For Loops Most imperative languages have a counting loop statement. A simple C/C++/Java-like counting loop is called a for loop. The concrete syntax of a for loop is as follows: ForStatement  for(Assignment1 opt ; Expression opt ; Assignment2 opt ) Statement

Do Statements Another type of looping construct is called do statement.  the body of the loop is guaranteed to execute at least once.  this is useful to initialize variables used in the loop test. Concrete syntax: DoStatement  do Statement while (Expression)

Switch Statements In addition to simple IfStatements, most imperative languages provide a multiway conditional statement such as switch statements. Concrete syntax: SwitchStatement  switch ( Expression ) { Cases } Cases  Case Cases | Case | Default Case  CaseHead Case | CaseHead Statements CaseHead  case Literal : Default  default : Statements

Switch Statements (2) Within the above constraints, we can model the SwitchStaement : switch(e){ case v 1 : s 1 … case v n : s n default: s n+1 } as a series of IfStatements : if(e==v 1 ) s 1 … else if (e==v n ) s n else s n+1