6
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

Embed Size (px)

Citation preview

Page 1: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

CSE1301Computer Programming

Lecture 5:C Primitives 2Corrections

Page 2: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

Arithmetic Expressions• take arithmetic (numerical) values and • return an arithmetic (numerical) value• Are composed using the following operators:

+ (unary plus)- (unary minus)+ (addition)- (subtraction)* (multiplication)/ (division or quotient)% (modulus or remainder)

Page 3: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

Unary operators

• Called unary because they require one operand.• Examples

i = +1; /* + used as a unary operator */

j = -i; /* - used as a unary operator */

• The unary + operator does nothing – just emphasis that a numeric constant is positive.

• The unary – operator produces the negative of its operand.

Page 4: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

Increment and decrement operators

• ++ is the increment operator i++;

is equivalent toi = i + 1;

• -- is the decrement operatorj--;

is equivalent toj = j - 1;

(King, pp53-54)

Page 5: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

Precedence in Expressions – Example

1 + 2 * 3 - 4 / 5 =

B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction.

B.O.D.M.A.S.1 + (2 * 3) - (4 / 5)

Page 6: CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections

More on precedence

• *, /, % are at the same level of precedence • +, - are at the same level of precedence • For operators at the same “level”, left-to-right

ordering is applied.2 + 3 – 1 = (2 + 3) – 1 = 42 – 3 + 1 = (2 – 3) + 1 = 0

2 * 3 / 4 = (2 * 3) / 4 = 6 / 42 / 3 * 4 = (2 / 3) * 4 = 0 / 4