1 / 9

Chapter 3 Arithmatic Operations 3.1 Operators

Chapter 3 Arithmatic Operations 3.1 Operators

ruthbrown
Download Presentation

Chapter 3 Arithmatic Operations 3.1 Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.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. OperatorOperationSample Expression + addition x + y - subtraction x - y * multiplication x * y / division x / y % remainder x % y

  4. 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.

  5. OperatorMeaning += add operand to variable and assign result to variable -= subtract operand from variable and assign result to variable *= multiply variable by operand and assign result to variable /= divide variable by operand and assign result to variable %= divide variable by operand and 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;

  6. 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;

  7. 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.

  8. NameDescription 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

  9. The function declaration for the 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

More Related