programming fundamental slides1 data types, identifiers, and expressions topics to cover here: data...

22
Programming Fundamental Slide s 1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical Expressions

Upload: merilyn-bond

Post on 04-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 1

Data Types, Identifiers, and Expressions

Topics to cover here:

Data types Variables and Identifiers Arithmetic and Logical Expressions

Page 2: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 2

Data Types

A data type provides a method of modeling or representing a particular set of values and determines what operations can be performed on those values.

Page 3: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 3

Data Types in The Algorithmic Language

Predefined Data Types: INT : values (e.g. 5 , -321 , 12) operations (e.g. +, -, *, /, , MOD, <, >, =, ≠, ≤, ≥ ) FLOAT : values (e.g. 3.2 , 1.23E+5 , 0.34E-2) operations (e.g. +, -, *, /, <, >, =, ≠, ≤, ≥) BOOL : values (true , false) operations (e.g. AND, OR, NOT) CHAR : values (e.g. ‘A’ , ‘t’ , ‘(‘ , ‘5’ , ‘;’ ) operations (e.g. <, > , ≤, etc.) String values (e.g. “ABC” , “computer”, “960”)NOTE: In C++ language, some of the operations have

different symbols.

Page 4: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 4

Identifiers, Locations, and Variables

Memory: It is a part of the computer that has many locations (of fixed size) used to store values (data) and instructions.

Page 5: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 5

Identifiers, Locations, and Variables.. cont.

Identifiers (Names): - Use meaningful identifiers (names) to represent data in your program. - Identifiers are used to refer to memory locations: to store data in them or retrieve data from them.

The rule of defining an identifier: (1) It may contain letters (A .. Z , a .. z ) , digits (0, 1, 2, …, 9), and underscore ( _ ). (2) It should begin with a letter.

Page 6: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 6

Identifiers, Locations, and Variables.. cont.

NOTES: Some programming languages are case

sensitive. That is the uppercase identifiers are different than lowercase identifiers (as in C++).

In programming languages, some words cannot be used as identifiers in your program. Such words are called reserved words (or keywords) that have special use.

Page 7: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 7

Identifiers, Locations, and Variables.. cont.

Examples of valid identifiers: area , length, X , Y1, abc, d3, st_number { these all begin with a letter }

Examples of invalid identifiers: 2Y { begins with a digit } Ali’s { contains the symbol ‘ } st-age { the symbol - is not underscore } while { it is a keyword } ab cd { it has a space }

Page 8: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 8

Identifiers, Location, and Variables.. cont.

Variables:-The name of the location is the variable name.-The location content is the value of the variable- You can change the content of the variable at any time in the statements of the algorithm.e.g. Name of the location Inside Memory location employee_name “Ali Ahmed” age 35 hourly_rate 3.25

Page 9: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 9

Identifiers, Location, and Variables.. cont.

Constants:

- You can use a constant identifier in your algorithm to indicate a constant data.

- You CANNOT change the content of the constant identifier.

- Use the keyword CONST to indicate a constant identifier.

e.g.

CONST pi = 3.14 Here, pi is a constant identifier that cannot be changed

Page 10: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 10

Declaring Identifiers

In the algorithmic language, we will use identifiers without declaring them

In C++, you should define any identifier used in in the program before using it in any statement of the program.

Ex: int y;

Page 11: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 11

Expressions Arithmetic Expression: - It is composed of operands and arithmetic operations ( + , - , *, /, MOD). - Its result is a numeric value (e.g. 3 + 4 gives 7) - Operands may be numbers and/or identifiers that

have numeric valuese.g. x – y gives 4, if x is 6 and y is 2 x / 2 gives 6, if x 12 T MOD 2 gives 0 if T is any even number, and 1 if T is any odd number

Page 12: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 12

Expressions .. cont. Logical Expression: - It is called also Boolean expression. - It is composed from operands and operators. - Operands are identifiers that have logical values - Its result is a logical value (true or false) (see

later). - Operators are logical: AND , OR, NOT

e.g. X AND Y a AND b OR c where X, Y, a, b, c are defined logical

Page 13: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 13

Expressions .. cont. Relational Expression: - It is composed from operands and operators. - Operands may be numbers and/or identifiers that have numeric values - Its result is a logical value (true or false). - Operators are relational operators: <, >, =, ≠, ≤, ≥e.g. (a < b) gives true, if value of a is less than value of b false, if value of a is not less than value of b

(x ≠ y) also gives true or false according to the values of x and y

Page 14: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 14

Expressions .. cont.

NOTES

1) A relational expression may contain arithmetic sub-expressions,

e.g. ( 3 + 7 ) < (12 * 4 )

2) A logical expression may contain relational and arithmetic sub-expressions,

e.g.

1- x AND y AND ( a > b )

2- (2 + t ) < (6 * w ) AND ( p = q )

Page 15: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 15

Operator Precedence

Expressions are evaluated according to the precedence rule.

Precedence Rule:

- Each operator has its own precedence that indicates the order of evaluation.

- If the expression has operators of the same precedence, then the evaluation starts from left of expression to the right.

Page 16: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 16

Operator Precedence .. cont.

OperatorDescriptionPrecedence

(parenthesesHigher

++, - – , NOT

unary plus, unary minus, Not

*, /, MOD

+ , - Binary plus, binary minus

<, <=, >, >=

= , !=Equal, not equal

AND

OR

Assignment Lower

Page 17: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 17

Examples

Find the value of the following expression:(1) 5 + 8 * 2 / 4 16

4

9 (This is the final result)

Page 18: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 18

Examples .. cont.

(2) ( 9 + 3 ) - 6 / 2 + 5

12

3

9

14 (this is the final result)

Page 19: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 19

Evaluating Logical Expressions

The truth table

(1) AND table

AND True False

True True False

False False False

Page 20: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 20

Evaluating Logical Expressions.. cont.

(2) OR table OR True False True True True False True False

(3) NOT table NOT True False False True

Page 21: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 21

Examples on Logical Expressions

(1) If x = True, y = False, z = False, find the value of the expression x AND y OR z

x AND y OR z

False

False (the final result)

Page 22: Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical

Programming Fundamental Slides 22

Examples on Logical Expressions .. cont.

(2) If a = 3, b = 5, x = true, y = false, find the value of the expression: ( a < b ) AND y OR x

( a < b ) AND y OR x

True

False

True (the final result)