Chapter 3 Arithmatic Operations 3 1 Operators An

  • Slides: 9
Download presentation
Chapter 3 Arithmatic Operations 3. 1 Operators An operator is a symbol or world

Chapter 3 Arithmatic Operations 3. 1 Operators An operator is a symbol or world that represents some operation that is performed on one or more data values. The data values are called operands and can be either variables or constants. For example, the addition operator, which is represented by the plus(+) sign, adds two numbers. The numbers to be added are the operands.

3. 2 Assignment Statements The assignment statement is the primary means of assigning values

3. 2 Assignment Statements The assignment statement is the primary means of assigning values to variables and for performing computations. The general form of the assignment statement is as follows: variable = arithmetic expression; For example: sum = sum + 3; The simplest form of assignment statement consists of the equal (=) symbol with a variable on the left and a constant on the right For example: x = 5;

3. 3 Arithmetic Operators C provides operators for performing arithmetic operations such as addition,

3. 3 Arithmetic Operators C provides operators for performing arithmetic operations such as addition, subtraction, multiplication, and division. Each operator is represented by a unique symbol. The arithmetic operations that can be performed and the corresponding symbols used for denoting the operation are shown as follows. Operator Operation + addition * subtraction multiplication / % division remainder Sample Expression x+y x-y x*y x/y x%y

3. 4 Arithmetic Assignment Operators All the arithmetic operators can be combined with the

3. 4 Arithmetic Assignment Operators All the arithmetic operators can be combined with the assignment operator. The general format of an arithmetic assignment is as follows. variable operator = operand; Where operator is an arithmetic such as +, -, *, /, or %. Examples of statements containing arithmetic assignment operators and the equivalent assignment statements are given as follows.

Operator Meaning =+ add operand to variable and assign result to variable =- subtract

Operator Meaning =+ add operand to variable and assign result to variable =- subtract operand from variable and assign result to variable =* multiply variable by operand assign result to variable =/ divide variable by operand assign result to variable =% divide variable by operand assign remainder to variable For Example: delta += 0. 5; delta = delta + 0. 5; new_val -= step_size; new_val = new_val - step_size; next_step *= 10; next_step = next_step * 10; zeta /= 100; zeta = zeta/100; a %= 2; a = a % 2;

3. 5 Increment and Decrement Operators A common programming task consists of increasing or

3. 5 Increment and Decrement Operators A common programming task consists of increasing or decreasing the value of a variable by 1. C has two operators, not usually found in other programming languages, to perform these tasks. They are the increment and decrement operators, ++ and --. The increment operator ++ adds one to its operand. For example, the statement x; ++ adds 1 to the value of x and is equivalent to x = x + 1; The decrement operator -- subtracts one from its operand. For example, the statement y; -y = y - 1;

We can put the increment and decrement operators either before the variable as in

We can put the increment and decrement operators either before the variable as in ++x; -- y; or after the variable as in x; ++ y; -3. 6 Mathematical funtions Mathematical functions such as square roots, absolute values, logarithms, trigonometric and exponential functions appear routinely in a variety of engineering applications. Since these functions are commonly used, all C compilers provide a set of mathematical functions in their standard library.

Name Description abs() absolute value of an int acos() arccosine asin() arcsine atan() arctangent

Name Description abs() absolute value of an int acos() arccosine asin() arcsine atan() arctangent cos() cosine cosh() hyperbolic cosine exp() exponential log() logarithm to the base 10 pow() x to the y power, xy sin() sine sinh() hyperbolic sine sqrt() square root tan() tangent tanh() hyperbolic tangent

The function declaration for the mathematical functions are included in the file math. h.

The function declaration for the mathematical functions are included in the file math. h. You should include this header file in all programs that use any of the mathematical functions. You can use the following preprocessor directive. Any Questions