COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts...

Post on 18-Jan-2016

214 views 0 download

Transcript of COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Manipulating Data Concepts...

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

2COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: Literal values

3COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

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

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

5COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: Data Types

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

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

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

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

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

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:

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…

13COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Summary: Literals and Data Types

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…

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

16COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

Manipulating Data

Concept: 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

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

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

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?

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

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

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?

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