26
Computer Science: A Structured Programming Approach Using C 1 Arithmetic In C Arithmetic In C

test(3)arithmetic in c

Embed Size (px)

DESCRIPTION

test

Citation preview

Page 1: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 1

Arithmetic In CArithmetic In C

Page 2: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 2

Arithmetic In C

To solve most programming problems, you need to write To solve most programming problems, you need to write arithmetic operations that manipulate type integer, float and arithmetic operations that manipulate type integer, float and double data. double data. C enables programmers to perform all types of arithmetic C enables programmers to perform all types of arithmetic calculations. Most common used arithmetic operators are:calculations. Most common used arithmetic operators are:

Page 3: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 3

Page 4: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 4

Expression

•An expression is a sequence of operands and An expression is a sequence of operands and operators that reduces to a single value. operators that reduces to a single value. Expressions can be simple or complex. Expressions can be simple or complex. •An operator is a syntactical token that requires an An operator is a syntactical token that requires an action be taken. action be taken. •An operand is an object on which an operation is An operand is an object on which an operation is performed; it receives an operator’s action.performed; it receives an operator’s action.

Page 5: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 5

Expression

The equal sign (=) is an assignment operator where The equal sign (=) is an assignment operator where the right side is being assigned to the left side of the the right side is being assigned to the left side of the operator (=) .operator (=) .

Operand1Operand1 Operand2Operand2+ResultResult =

Page 6: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 6

Both operands of the modulus operator (%) must be integer types.

NoteNote

Page 7: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 7

Page 8: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 8

Page 9: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 9

Page 10: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 10

Converting Algebra FormulasTo C Formulas

•Always specify multiplication with the symbol *.•C explicitly require the multiplication operator. Example: a*b ( cannot put it as ab )•Use parentheses when required to control the order of operator evaluation.

Page 11: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 11

Converting Algebra FormulasTo C Formulas

Page 12: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 12

Precedence and Associativity

•Precedence is used to determine the order in which Precedence is used to determine the order in which different operators in a complex expression are different operators in a complex expression are evaluated. evaluated. •Associativity is used to determine the order in which Associativity is used to determine the order in which operators with the same precedence are evaluated in a operators with the same precedence are evaluated in a complex expression. complex expression.

Left-to-Right Associativity

Page 13: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 13

Page 14: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 14

Page 15: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 15

Expression Evaluation

Page 16: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 16

Expression Evaluation

Page 17: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 17

Expression Evaluation

Page 18: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 18

Compound Expression

Compound Compound ExpressionExpression

Equivalent Simple Equivalent Simple ExpressionExpression

X*=3 X=X*3

X/=3 X=X/3

X%=3 X=X%3

X+=1 X=X+1

X-=1 X=X-1

Page 19: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 19

Increment and Decrement Operator

Increment Decrement

Prefix Increment Postfix DecrementPrefix DecrementPostfix Increment

++a a++ --a a--

Increment a by 1then use the new value of a in the expression.

Use the current value of a in the expression, then increment a by 1.

Decrement a by 1then use the new value of a in the expression.

Use the current value of a in the expression, then decrement a by 1.

Page 20: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 20

Demonstrate Postfix Increment

Page 21: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 21

Demonstrate Postfix Increment (continued)

Page 22: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 22

Demonstrate Prefix Increment

Page 23: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 23

Demonstrate Prefix Increment (continued)

Page 24: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 24

Evaluating Expressions

Page 25: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 25

Evaluating Expressions

Page 26: test(3)arithmetic in c

Computer Science: A Structured Programming Approach Using C 26

Evaluating Expressions