Expressions Statements DataTypes 1 Chapter 3 Operators and

  • Slides: 29
Download presentation
Expressions, Statements & Data-Types 1 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique

Expressions, Statements & Data-Types 1 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1

Elements of a program n Literals fixed data written into a program n Variables

Elements of a program n Literals fixed data written into a program n Variables & constants placeholders (in memory) n n for pieces of data Types sets of possible values for data Expressions combinations of operands and operators. They compute new values from old ones. Assignments used to store values into variables Statements "instructions". In C, any expression followed by a semicolon is a statement 2 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 2

Elements of a program n Control-flow constructs that allow statements or groups of statements

Elements of a program n Control-flow constructs that allow statements or groups of statements to be executed only when certain conditions hold or to be executed more than once. n Functions named blocks of statements that perform a well-defined operation. n Libraries collections of functions. 3 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 3

Statement n Statements are elements in a program which (usually) ended up with semi-colon

Statement n Statements are elements in a program which (usually) ended up with semi-colon (; ) n e. g. below is a variables declaration statement int a, b, c; • Preprocessor directives (i. e. #include and define) are not statements. They don’t use semi-colon 4 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 4

Some types of statement in C++ 5 Chapter 3: Operators and Expressions| SCP 1103

Some types of statement in C++ 5 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 5

An expression statement is a statement that results a value Some examples of expression

An expression statement is a statement that results a value Some examples of expression Value • Literal expression e. g. 2, “A+”, ‘B’ The literal itself • Variable expression e. g. Variable 1 The content of the variable • arithmetic expression e. g. 2 + 3 -1 The result of the operation 6 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 6

Operators n Operators can be classified according to n the type of their operands

Operators n Operators can be classified according to n the type of their operands and of their output n Arithmetic n Relational n Logical n Bitwise n the number of their operands n Unary (one operand) n Binary (two operands) 7 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 7

Binary expression 8 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C |

Binary expression 8 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 8

Unary Expression 9 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C |

Unary Expression 9 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 9

Ternary Expression First operand is a condition Second operand is a value Third operand

Ternary Expression First operand is a condition Second operand is a value Third operand is another value (a>2) ? 1: 0 Operator 10 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 10

Arithmetic operators n They operate on numbers and the result is a number. n

Arithmetic operators n They operate on numbers and the result is a number. n The type of the result depends on the types of the operands. n If the types of the operands differ (e. g. an integer added to a floating point number), one is "promoted" to other. n The "smaller" type is promoted to the "larger" one. char int float double 11 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 11

Example of promotion: The result of the following “double division” is 2. 5 5

Example of promotion: The result of the following “double division” is 2. 5 5 / 2. 0 Before the division process, 5 is promoted from integer 5 to float 5. 0 The result of the following “integer division” is 2 5/2 There is no promotion occurred. Both operands are the same type. 12 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 12

Arithmetic operators: +, * n + is the addition operator n * is the

Arithmetic operators: +, * n + is the addition operator n * is the multiplication operator n They are both binary 13 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 13

Arithmetic operator: n This operator has two meanings: n subtraction operator (binary) e. g.

Arithmetic operator: n This operator has two meanings: n subtraction operator (binary) e. g. 31 - 2 n negation operator (unary) e. g. -10 14 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 14

Arithmetic operator: / n Division operator n CAREFUL! The result of integer division is

Arithmetic operator: / n Division operator n CAREFUL! The result of integer division is an integer: e. g. 5 / 2 is 2, not 2. 5 15 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 15

Arithmetic operator: % n The modulus (remainder) operator. n It computes the remainder after

Arithmetic operator: % n The modulus (remainder) operator. n It computes the remainder after the first operand is divided by the second e. g. 5 % 2 is 1, 6 % 2 is 0 16 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 16

Relational operators n These perform comparisons and the result is what is called a

Relational operators n These perform comparisons and the result is what is called a boolean: a value TRUE or FALSE n FALSE is represented by 0; anything else is TRUE n The relational operators are: n n n < <= > >= == != (less than) (less than or equal to) (greater than or equal to) (not equal to) 17 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 17

18 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

18 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 18

Logical operators (also called Boolean operators) n These have Boolean operands and the result

Logical operators (also called Boolean operators) n These have Boolean operands and the result is also a Boolean. n The basic Boolean operators are: n n n && (logical AND) || (logical OR) ! (logical NOT) -- unary 19 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 19

20 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

20 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 20

Assignment operator: = n Binary operator used to assign a value to a variable.

Assignment operator: = n Binary operator used to assign a value to a variable. 21 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 21

Special assignment operators n write n write a a a += -= *= /=

Special assignment operators n write n write a a a += -= *= /= %= b; instead of a b; instead of a = = = a a a + * / % b; b; b; 22 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 22

23 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

23 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 23

24 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

24 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 24

25 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

25 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 25

26 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

26 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 26

27 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

27 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 27

28 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

28 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 28

29 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM,

29 Chapter 3: Operators and Expressions| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 29