cop 2510 programming conceptsalessio gaspar bsas industrial operations 1 manipulating data concepts...

24
1 COP 2510 Programming Concepts Alessio Gaspar BSAS Industrial Operations Manipulating Data Concepts Covered: Literal values, variables and Types Variables declarations Variables assignments Input / Output operations Statements Part 1 Module

Upload: posy-harrison

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

1COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concepts Covered:

Literal values, variables and TypesVariables declarationsVariables assignmentsInput / Output operations Statements

Part 1Module 1

Page 2: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

2COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: Literal values

Page 3: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

3COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Give me some examples of values you might use in everyday life:

Page 4: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

4COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

These are possible answers to the previous question…

• 20ft • 32˚F• 103.27cm• 6.02 . 1023 • "Alessio Gaspar"• "COP 2510 Programming Concepts"

We can easily categorize them:– Numerical value, Strings, …– This leads us to the concept of data type

Page 5: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

5COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: Data Types

Page 6: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

6COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• Numerical Integer numbers in base 10Integer numbers in other

bases (octal, hexadecimal, binary)

Floating point numbers in decimal notation

Floating point numbers in scientific notation

• Strings

20ft32˚F

103.27cm

6.02 . 1023

"COP 2510 Programming Concepts"

Values belong to different TYPES

Page 7: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

7COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• Numerical – Integer numbers in base 10

– Integer numbers in other bases (octal, hexadecimal, binary)

– Floating point numbers in decimal notation

– Floating point numbers in scientific notation

• Strings

Values belong to different TYPES

Page 8: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

8COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Value

String Numerical

Integer FloatingPoint

• Numerical Integer numbers in base 10Integer numbers in other

bases (octal, hexadecimal, binary)

Floating point numbers in decimal notation

Floating point numbers in scientific notation

• Strings

Values belong to different TYPES

Page 9: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

9COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Value

String Numerical

Integer FloatingPoint

This view of types is a little

“raw”

• Numerical – Integer numbers in base 10

– Integer numbers in other bases (octal, hexadecimal, binary)

– Floating point numbers in decimal notation

– Floating point numbers in scientific notation

• Strings

Values belong to different TYPES

Page 10: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

10COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Value

String Numerical

Integer FloatingPoint

This is closer to the kind of “types” we use

everyday in real life

• Numerical – Temperatures

– Distances

– Duration

– Quantity

• Strings– Employee Last Name

– Street Name

– Whole Address

Values belong to different TYPES

Page 11: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

11COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• Valid Ranges– Smallest / biggest possible value

– E.g. $1,000,000 in a bank account is suspicious for me

– E.g. -273 Celsius degrees on your thermometer is suspicious too

• Encodings– 0010 is binary for the decimal value

2

– 3 is decimal for 0011 in binary

– A is hexadecimal for 10

Value

String Numerical

Integer FloatingPoint

(more about this later…)

Values can have a meaning (“semantics”) which dictates characteristics such as:

Page 12: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

12COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• Literal value: i.e. How do we write a value in a computer program?

– 10 vs. 10.0 – 20.3 – “what are you doing in my swamp?” – 0xA same as 10 s.a. 011 s.a. 1010

• Idea of type – Integer number: short, int, long…– Floating point number: float, double,

long double… – Single character: ‘a’– String: “this is a string”– Each with SYNTACTICAL notations

and range values

When manipulating data in a computer, we capture some of these everyday life practices

“some” not “all”Values in a program are typed

in a formal way

We’ll see very soon why it is so…

Page 13: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

13COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Summary: Literals and Data Types

Page 14: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

14COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

What we have so far

• Literal values can be written in computer programs by following some syntactical conventions

• These values belong to data types which the programming language can manipulate and, as such, have characteristics such as the range of possible values, the lowest value, the highest value…

Page 15: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

15COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

So… • We can imagine that programming languages will allow us to

write operations on these values E.g. Compute 2 + 2

• The results can be – Displayed / written to disk / sent over the internet to another

program…

But…• what about applications which need to keep results of previous

operations handy? – A word processor keeps the text you’re writing in memory

somewhere until you close the application– A budget software will store your expenses, compute their sum,

keep the value of your balance in memory • We need to introduce another programming concept…

variables

Page 16: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

16COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: Variables

Page 17: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

17COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

What is the purpose of a variable?

• To Store data

More specifically, each variable stores

one value at a time

This allows you to use

the result of a computation later on

Page 18: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

18COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

How do programming languages let you use variables in your programs?

Assigning values to variables

• Here are examples of different syntax that have been used over the years in various languages:

A := 2 + 3 A 2 + 3A = 2 + 3

Page 19: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

19COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

How do programming languages let you use variables in your programs?

Assigning values to variables

• Here are examples of different syntax that have been used over the years in various languages:

Variable Name

AssignmentOperator

Expression Resulting in

a Value

Generic Syntactical Diagram: A := 2 + 3 A 2 + 3A = 2 + 3

Page 20: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

20COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Doing i/o operations with variables

• Displaying the value of a variable on the screen

• Displaying the result of an expression combining variables and literal values on the screen

• Reading a value from the user (keyboard) and storing it in a variable

How do programming languages let you use variables in your programs?

Page 21: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

21COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

What is a variable in a computer program?

The 2 last items relate to the idea of encoding

An identifier (a name) to refer to it more easily than by having to specify the memory address at which it’s located

A TYPE which determines- How much memory is needed to store the value- How to read this value

Some space in memory to store a value in it– SIZE of this memory space

– ADDRESS at which it’s located

Page 22: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

22COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• All this can be specified when declaring variables

• The idea of TYPE is related to how values are stored in computer memory

Declaring Variables

Page 23: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

23COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

• All variables we discussed so far can hold only a single value at a time

• We will discuss in an upcoming module how we can group values together in aggregate variables

Going a little further: Grouping Variables?

Page 24: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts Covered: Literal values, variables and Types Variables

24COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Statements

We saw some examples of statements: • Declare a variable• Assign a value to a variable

Now what?

• Learning to program, regardless of the programming language, will require us to get familiar with typical available statements