1 / 17

Arithmetic Operators

Chapter 6. Arithmetic Operators. By: Mr. Baha Hanene. LEARNING OUTCOMES. This chapter will cover the learning outcome 02 i.e. 2. Use basic data-types and input / output in C programs (L02). CONTENTS. Basic Arithmetic Operators Operator Precedence Typecasting

kitty
Download Presentation

Arithmetic 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 6 Arithmetic Operators By: Mr. BahaHanene

  2. LEARNING OUTCOMES • This chapter will cover the learning outcome 02 i.e. • 2. Use basic data-types and input / output in C programs (L02)

  3. CONTENTS • Basic Arithmetic Operators • Operator Precedence • Typecasting • Increment & Decrement Operators • Relational Operators • Logical Operators

  4. BASIC ARITHEMATIC OPERATORS

  5. OPERATOR PRESEDENCE Note: All mathematical expressions are solved from left to right. So the operators having the same priority should be treated from left to right in order. Whatever the first should be solved first.

  6. OPERATOR PRECEDENCE PRACTICE X=5 + 3 * 6 Answer: 23 X=5 + 3 * 6 / 2 Answer: 14 X=(10 + 3) * 6 / 2 Answer: 39 X=(10 + 2) % 2 * (6 / 3) Answer: 0 X=(10 – 2 + 4) % 2 * 6 / 4 % 2 Answer: 0 X=5 * 3 + 6 Answer: 21 X=5 * 3 + 6 % 4 % 2 Answer: 15

  7. OPERATOR PRECEDENCE PRACTICE Let us consider some examples of real expressions & mixed expressions with floating point types. (Assume num=3) x = (2.1 + num) / 2 * 5 Ans. : 0.51 x = (2.1 + num) / (2 * 5) Ans. : 12.75 x = num / 2 Ans. : 1 Consider x is integer Ans. : 1 x = num / 2 Ans. : 1 x = num / 2.0

  8. COMPARE THE FOLLOWIG Exp. Suppose x=2 & y=4 y = x * x + 2 * x - 5 * x / y ; y = x * x + 2 * x - 5 * x / y ; Their answers will be same or different? (When writing complex arithmetic expressions, the spaces have no meaning, so the above two expr. are equivalent)

  9. TYPECASTING If both values are integer, the integer arithmetic is applied and the answer will be integer only, e.g. a = 10 / 3 Ans.: 3 (Whereas ‘a’ is integer) If both values are float, the floating point arithmetic is applied and the answer will be floating point only, e.g. a = 10.00 / 3.00 Ans.: 3.333333 (Whereas ‘a’ is float) If one value is integer & other is floating point, then the integer is converted to a floating point representation & floating point arithmetic is used, e.g. a = 10 / 3.0 (3.333333) OR a = 10.0 / 3 (3.333333) a = 10 / 3 (3.000000) (Whereas ‘a’ is float)

  10. TYPECASTING • However, it is possible to force or cast a variable to a • different type. We do typecasting in C language by • placing the type name in the parenthesis and putting it • in front of the value we want to change, e.g. If b is a float variable b = 10 / 3 Ans. 3.000000 b = (float) 10 / 3 Ans. 3.333333 We will get 3.333333, because 10 is converted to floating point value before division. NOTE: The type of left hand side variable has no effect on the calculation of the right hand side expression. This is because expression is always calculated before the assignment operation can take place.

  11. INCREMENT & DECREMENT OPERATORS ++a “pre-increment” a++ “post-increment” --a “pre-decrement” a-- “post-decrement”

  12. RELATIONAL OPERATORS • Relational operators allow comparison between two values giving a result whose value depends whether the comparison is true or false. • The result is 1 if the comparison is true and 0 if it is false. • The relational operators in C are: • == equal • != not equal • > greater than • >= greater than or equal • < less than • <= less than or equal • You must not confuse the == operator which compares for equality with the = which is the assignment operator.

  13. RELATIONAL OPERATORS • The following table explains how the relational operators are evaluated: • (3 == 3) true Result will be 1 • (3 != 3) false Result will be 0 • (3 > 3) false Result will be 0 • (3 >= 3) true Result will be 1 • (3 < 3) false Result will be 0 • (3 <=3) true Result will be 1 • Therefore the following program segment: • int num1 = 3, num2 = 5, result; • result = (num1 == num2); • printf(“%d”, result); • gives a value of zero.

  14. LOGICAL OPERATROS • Logical operators connect two or more logical expressions to achieve more powerful expressions. • There are three logical operators: • ! not • && and • || or • Each logical operator has a truth table • The truth table for not is: • !true false • !false true

  15. LOGICAL OPERATROS • The truth table for and is: true &&true true true && false false false && true false false && false false • The truth table for or is: true || true true true || false true false || true true false || false false

  16. LOGICAL OPERATROS • Check the following statements assuming that int x=5, y=8, z=3; • result = (x + z == y) && (x > z); /* result = true */ • result = (x != y) && (y != z); /* result = true */ • result = (x + z == y) && (x >= y); /* result = false*/ • result = (x + z == y) || (x >= y); /* result = true */ • result = !(x + z == y); /* result = false*/ Note: All logical operations will either result in 1 or 0 i.e. True or False

More Related