1 DAI-C5-UD 1 Main Objects in Programming Languages Academic Year 10-11 DAI. Credit 5 (Structured and modular programming) Ferran Chic (Ref: TR-5-1-01)

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CIS Computer Programming Logic
Stack Applications.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Primitive Variables.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Chapter 2 - Introduction to C Programming
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 2 - Introduction to C Programming
Operators and Expressions
Data Types, Identifiers, and Expressions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Relational Operators Operator Meaning < Less than > Greater than
Chapter 2 - Introduction to C Programming
Chapter 2 Variables.
Chapter 2 - Introduction to C Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions.
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Chapter 2 Primitive Data Types and Operations
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
COMPUTING.
Presentation transcript:

1 DAI-C5-UD 1 Main Objects in Programming Languages Academic Year DAI. Credit 5 (Structured and modular programming) Ferran Chic (Ref: TR )

2 Main Objects in Programming Languages Literals Constants Variables Elementary data types Type conversions Expressions Input/Output operations

3 Literals Numericals Integer (signed or unsigned) Real -3e e Character ‘a’ ‘1’ ‘x’ ‘!’ Boolean True/false String “HELLO WORLD” PELE-08/09 (Impuls Llengua Anglesa FP IES MONTILIVI) © Ferran Chic. DAI-1er- C5 (Programació Estructurada I Modular)

4 Constants A constant value can not change its value from the beginning until the end of the program execution. Added to all this, the value is set within the code. It can not be assigned during run time. Consists in associating an specific name with a literal Constant declaration can be done as follows const PI:real = DESCOMPTE:integer=15 fconst Therefore, the general format of a constant declaration is NAME:datatype=value

5 Variables A variable (as well as a constant) can store a single value Variables can change their value during program execution Variables and constants are identified by a name (also called identifier) Naming rules : constants and variables identifiers must start with a letter followed by a combination of letters and numbers and the special character _ Any other special character is not allowed

6 Elementary Data Types Data type is a key concept in programming Every single object (variable o constant) belongs to an specific data type Data type determines Which values can be stored Which operations can be performed with the objects (calculations) In pseudocode we have 4 basic data types integer real character boolean

7 Data Types The data type of a variable/constant fixes the set of values that the object can store How do we declare a variable ? var a,b:real c:integer fvar

8 Basic Operations With Variables Initialization: A variable does not contain any value until it is initialized. The first time that a variable has a value is called variable initializing Assignment: This operations is useful to change the value of a variable. We use operator := to make an assignment Examples: a := 3 b :=8*a On the left hand side of the assignment sentence we write the identifier of a variable On the right hand side we can write some calculation

9 Operations with variables (II) Use of the stored value: Whenever appears the name of a variable on the right side of an expression, we have to substitute the variable with it’s value. EXAMPLES a :=5 // Initialization d := a+3 //assignment with use of the stored //value of a i :=0 Initialization i :=i+1 //assignment with use of the stored //value of i

10 Data Types and possible values Possible values of a constant or a variable depend on it’s Type Examples Integer Data Type: Real Data Type: e20 Character Data Type (values single-quoted ‘ ‘): ‘a’ ‘A’ ‘#’ ‘3’ Boolean Data Type: true or false only

11 Integer Operators We can combine integer operands using integer operators obtaining integer results Product * Integer division div Remindermod Addition + Unary Substraction -a Binary Substraction a-b

12 Operations with integers Algorisme calculs var a,b,c,d,e,f,g:enter fvar a:=5 b:=2 c:= 5*a d:= a mod b e:= a div b f:= a +b- c + 1 g:= a+b*2div 4 +b*a falgorisme Write down the trace of this algorithm indicating the value of the variables on every instruction

13 Real Operators Real division : / Product : * Addition : + Unary substraction : -a Binary substraction : a-b Mathematical built in functions : sin(a), cos(a), sqrt(a),.....

14 Operator Preference Rules Rule 1: Priority Level 1  - (unary) Level 2  *, div, mod, / Level 3  +, - Rule 2: associativity Operators of the same level appearing in the same expression must be evaluated from left to right Left to right behaviour could be changed using parenthesis

15 Boolean Operators Boolean operations always value true or false To obtain a boolean result we can combine “non boolean operands” (relational operators) The most important relational operators are :, =, >, > = Meaning of the operations is the same as in algebra: 3>2 values true -7>10 values false 3.89 > values true ‘a’<‘k’ values true ‘a’=‘A’ values false

16 Boolean Operations (Example) Algorisme AngleObtús const ARECTE:real=90.0 fconst var myAngle:real esObtus:booleà fvar myAngle:=93.7 esObtus:= myAngle>ARECTE Falgorisme

17 Boolean Operators (II) Other operators combine boolean operators obtaining a boolean value as a result Assume that A i B are boolean expressions ABNot(A)A and BA or B FFTFF FTFT TFFFT TTTT

18 Operator precedence rules () Parenthesis. Tables and records access - (unary not and sign change) * / div mod + - (two operands addition and substraction) ≤ ≥ = ≠ operadors relacionals and (logical operator) or (logical operator)

19 Example On every expression, you have to decide Data type of x Values of x that make the expression true ((x>=2) and (x<=4)) (x mod 2 = 1) (x>=‘a’) and (x<=‘z’) ((x>=‘a’) and (x =‘A’) and (x<=‘Z’))

20 Is it always possible to assign a bigger set to a smaller set ? Is it possible to assign an integer to a real ? Is it possible the other way round ? Is it possible to assign long  int ? Is it possible the other way round? Is it possible to assign an integer to a char ? KEY CONCEPT: Casting when we should use casting conversions ? Data Type Conversions

21 Input/Output Operations INPUT OPERATIONS With input operations you will be able to get values from some input source (keyboard, file,...) An input operation assigns the value obtained from keyboard to a variable (acts in the same way as an assignment) i:= llegirEnter() //i is an integer var x:= llegirReal() // x is a real var c:= llegirCaracter() //c is a char var

22 OUTPUT OPERATIONS: Output operations allow us to send information to some output source (screen, printer...) escriureEnter(234) escriureReal(4.78) escriureCaracter(‘m’) escriureEnter(i) escriureEnter(i*2+340) escriureFrase(“El Diàmetre val “,d,”metres”) Input/Output Operations (II)