test(3)arithmetic in c

Preview:

DESCRIPTION

test

Citation preview

Computer Science: A Structured Programming Approach Using C 1

Arithmetic In CArithmetic 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:

Computer Science: A Structured Programming Approach Using C 3

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.

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 =

Computer Science: A Structured Programming Approach Using C 6

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

NoteNote

Computer Science: A Structured Programming Approach Using C 7

Computer Science: A Structured Programming Approach Using C 8

Computer Science: A Structured Programming Approach Using C 9

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.

Computer Science: A Structured Programming Approach Using C 11

Converting Algebra FormulasTo C Formulas

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

Computer Science: A Structured Programming Approach Using C 13

Computer Science: A Structured Programming Approach Using C 14

Computer Science: A Structured Programming Approach Using C 15

Expression Evaluation

Computer Science: A Structured Programming Approach Using C 16

Expression Evaluation

Computer Science: A Structured Programming Approach Using C 17

Expression Evaluation

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

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.

Computer Science: A Structured Programming Approach Using C 20

Demonstrate Postfix Increment

Computer Science: A Structured Programming Approach Using C 21

Demonstrate Postfix Increment (continued)

Computer Science: A Structured Programming Approach Using C 22

Demonstrate Prefix Increment

Computer Science: A Structured Programming Approach Using C 23

Demonstrate Prefix Increment (continued)

Computer Science: A Structured Programming Approach Using C 24

Evaluating Expressions

Computer Science: A Structured Programming Approach Using C 25

Evaluating Expressions

Computer Science: A Structured Programming Approach Using C 26

Evaluating Expressions

Recommended