Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Data types and variables
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.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Data types and their representation Jordi Cortadella Department of Computer Science.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chars and strings Jordi Cortadella Department of Computer Science.
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.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CS161 Introduction to Computer Science Topic #6.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
 Type Called bool  Bool has only two possible values: True and False.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
Jordi Cortadella Department of Computer Science
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2: Problem Solving Using C++
Multiple variables can be created in one declaration
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
C Operators, Operands, Expressions & Statements
Chapter 2: Introduction to C++.
The Data Element.
The Data Element.
Chapter 2 Primitive Data Types and Operations
Programming Fundamental-1
Presentation transcript:

Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC

Outline Data types Type conversion Visibility 2© Dept. CS, UPCIntroduction to Programming

Data types A data type specifies: – The set of values that data of that type can have (e.g. integer, real, character, Boolean, colour, Greek letter, city, etc.) – The type of operations that can be performed with the data. For example, two integer numbers can be added, the population of a city can be calculated, etc. Introduction to Programming© Dept. CS, UPC3

Basic data types in C++ (int) Integer (int). Represent the set of integer numbers. – In practice, computers have a limitation representing integer numbers. – For a 32-bit machine, int can represent the numbers in the interval [-( ), ]. [ , ] – Arithmetic operators: +, -, , /, % Integer division and remainder: 13 / 3 = 4, 13 % 3 = 1 Introduction to Programming© Dept. CS, UPC4

Basic data types in C++ (double) Real (double). Represent the set of real numbers. – In practice, computers can only represent real numbers in a certain interval and with a certain accuracy. – IEEE standard, double-precision 64 bit: Numbers closest to zero: ±5 × 10 −324 Numbers furthest from zero: ± × Special representations for 0, +  and -  See – Arithmetic operators: +, -, , / Real division: 13.0 / 4.0 = 3.25 Introduction to Programming© Dept. CS, UPC5

Basic data types in C++ (bool) Boolean (bool). Represent logic values. – Values: false and true – Operators: not, and, or. Introduction to Programming© Dept. CS, UPC6 xnot x falsetrue false xyx and y false truefalse truefalse true xyx or y false true falsetrue

Basic data types in C++ (bool) Properties of Boolean algebra – Commutativity: a and b = b and a a or b = b or a – Associativity: (a and b) and c = a and (b and c) (a or b) or c = a or (b or c) – Distributivity: a and (b or c) = (a and b) or (a and c) a or (b and c) = (a or b) and (a or c) – Double negation: not (not a) = a – De Morgan’s law: not (a and b) = (not a) or (not b) not (a or b) = (not a) and (not b) Introduction to Programming© Dept. CS, UPC7

Basic data types in C++ (char) Character (char). Represent letters, digits, punctuation marks and control characters. Every character is represented by a code (integer number). There are various standard codes: – American Standard Code for Information Interchange (ASCII) – Unicode (wider than ASCII) Some characters are grouped by families (uppercase letters, lowercase letters and digits). Characters in a family have consecutive codes: 'a'…'z', 'A'…'Z', '0'…'9' Operators: given the integer encoding, arithmetic operators can be used, even though only addition and subtraction make sense, e.g. 'C'+1='D', 'm'+4='q', 'G'-1='F'. Introduction to Programming© Dept. CS, UPC8

Basic data types in C++ (char) Introduction to Programming© Dept. CS, UPC9 ASCII code

Basic data types in C++ (string) Strings (string). Represent sequences of characters. Examples – "Hello, world!", "This is a string", ":-)", "3.1416" – "" is the empty string (no characters) – 'A' is a character, "A" is a string Note: use #include in the header of a program using strings. Introduction to Programming© Dept. CS, UPC10

Relational operators The values of most data types can be compared using relational operators: == != > >= < <= Relational operators return a Boolean value (true or false) Examples – 5 == 5 is true, 5 == 6 is false, 5 != 6 is true – = 0.1 is false – 'J' <= 'K' is true, 'a' == 'A' is false – "Obama" == "Bush" is false, "Bush" == "Bush" is true, "Bush" < "Obama" is true, "book" < "booking" is true (relational operators use lexicographical order in strings) Introduction to Programming© Dept. CS, UPC11

Variable declarations A variable is declared as: type variable_name; Examples int population; double distance; string my_name; Several variables can be declared together: int age, children, cars; After its declaration, the value of a variable is undefined (unknown). Introduction to Programming© Dept. CS, UPC12

Expressions Expression: a combination of literals, variables, operators and functions that is evaluated and returns a value Examples: a + 3(i - 1)  int sqrt(x)log(4n)  double (i - 3) <= x  bool (a != b) and (s <= "abc")  bool Introduction to Programming© Dept. CS, UPC13

Expressions The operands used in expressions must be consistent with the operators. int a, b, n; … (a <= b) + n (Incorrect expression: semantic error) Introduction to Programming© Dept. CS, UPC14 bool int cannot add bool to int

Expressions Operators in expressions are evaluated according to certain rules of precedence Example:  5 != (3 + 4)  5 Use parenthesis to change the precedence or when you are not sure about it. Introduction to Programming© Dept. CS, UPC15 Unary+, -, not Multiplicative  / % Additive+ - Relational (inequalities)> >= < <= Relational (equalities)== != Conjunctionand Disjunctionor

TYPE CONVERSION Introduction to Programming© Dept. CS, UPC16

Type conversion Consider the following code: int i = 5; char a = ‘B’; double x = 1.5; i = i + x; if (i) x = 5a; Introduction to Programming© Dept. CS, UPC17

Type conversion In many programming languages, the compiler would report several type errors. Possibly: int i = 5; char a = ‘B’; double x = 1.5; i = i + x; if (i) x = 5a; Introduction to Programming© Dept. CS, UPC18

Type conversion In C++, there would be no errors in this fragment of code: int i = 5; char a = ‘B’; double x = 1.5; i = i + x; // i gets the value 6 if (i) x = 5a; // the condition of the if statement // would be true and x would get 5 // multiplied by the code of ‘B’ // converted into double Introduction to Programming© Dept. CS, UPC19

Type conversion As a general rule, using implicit type conversions is not considered to be a good practice because: – The code is less readable. – The code is less reliable, since unintentional errors may be introduced and they may be difficult to debug. Recommendation: to operate with different types, use explicit type conversions char(i), int(‘a’), double(i) Never use statements that depend on a particular encoding: – Wrong: c == 65, c == char(65), int(c) == 65 – Correct: c == ‘A’ Introduction to Programming© Dept. CS, UPC20

Type conversion Arithmetic operations between integer and real values usually imply an implicit conversion into real values. Be careful: int i=3, j=2; double x; x = i/j; // x = 1.0 x = i/double(j); // x = 1.5 x = double(i)/j; // x = 1.5 x = double(i/j); // x = 1.0 x = i/2; // x = 1.0 x = i/2.0; // x = 1.5 Introduction to Programming© Dept. CS, UPC21

VISIBILITY Introduction to Programming© Dept. CS, UPC22

Visibility of variables Variables are only visible after their declaration and in the block they have been declared. Blocks can include other blocks. The variables of the outer blocks are visible, a priori, in the inner blocks. A variable declared in an inner block masks the variables with the same name declared in outer blocks. Introduction to Programming© Dept. CS, UPC23

Visibility of variables { // a and b are not visible int a = 1, b = 20; // a and b are visible cout << a; // writes 1 { // c is not visible, a and b are visible cout << a + b; // writes 21 int b = 3, c = 4; // b and c are visible, // but the outer b is not visible cout << b; // writes 3 cout << c; // writes 4 } // c is not visible cout << b; // writes 20 } Introduction to Programming© Dept. CS, UPC24