By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02...

17
Arithmetic Operators By: Mr. Baha Hanene Chapter 6

Transcript of By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02...

Page 1: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

Arithmetic OperatorsBy:

Mr. Baha Hanene

Chapter 6

Page 2: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

2

LEARNING OUTCOMESThis chapter will cover the learning outcome

02 i.e.

2. Use basic data-types and input / output in C programs (L02)

Page 3: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

3

CONTENTSBasic Arithmetic OperatorsOperator PrecedenceTypecastingIncrement & Decrement OperatorsRelational OperatorsLogical Operators

Page 4: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

4

BASIC ARITHEMATIC OPERATORSOperators Description

+ Used for Addition Operation

- Used for Subtraction Operation

* Used for Multiplication Operation

/ Used for Division Operation

% Used for Remainder Operations

Page 5: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

5

OPERATOR PRESEDENCEOperators Precedence

( ) Top priority in any expression is given to these small parentheses.

* / % Second priority in any expression is given to * / % operators whatever the first should be solved first.

+ - Last priority in any expression is given to + - operators whatever the first should be solved first.

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.

Page 6: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

6

OPERATOR PRECEDENCE PRACTICEX=5 + 3 * 6X=5 + 3 * 6 / 2X=(10 + 3) * 6 / 2X=(10 + 2) % 2 * (6 / 3) X=(10 – 2 + 4) % 2 * 6 / 4 % 2X=5 * 3 + 6X=5 * 3 + 6 % 4 % 2

Answer: 23

Answer: 14

Answer: 39

Answer: 0

Answer: 0

Answer: 21

Answer: 15

Page 7: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

7

OPERATOR PRECEDENCE PRACTICE

x = (2.1 + num) / 2 * 5 Ans. : 0.51

x = (2.1 + num) / (2 * 5) Ans. : 12.75

x = num / 2 Ans. : 1

Let us consider some examples of real expressions & mixed expressions with floating point types. (Assume num=3)

Consider x is integer

x = num / 2 Ans. : 1 x = num / 2.0 Ans. : 1

Page 8: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

8

COMPARE THE FOLLOWIG Exp.Suppose x=2 & y=4y = 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)

Page 9: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

9

TYPECASTINGIf 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)

Page 10: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

10

TYPECASTINGHowever, 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 = (float) 10 / 3 Ans. 3.333333b = 10 / 3 Ans. 3.000000

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.

Page 11: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

11

INCREMENT & DECREMENT OPERATORS

Operators

Description

++a add one to (a) before using it

a++ add one to (a) after using it

--a subtract one from (a) before using it

a-- subtract one from (a) after using it++a “pre-increment” a++ “post-increment”--a “pre-decrement” a-- “post-decrement”

Page 12: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

12

Page 13: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

13

RELATIONAL OPERATORSRelational 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 equalYou must not confuse the == operator which compares for

equality with the = which is the assignment operator.

Page 14: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

14

RELATIONAL OPERATORSThe 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 1Therefore the following program segment: int num1 = 3, num2 = 5, result; result = (num1 == num2); printf(“%d”, result); gives a value of zero.

Page 15: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

15

LOGICAL OPERATROSLogical operators connect two or more

logical expressions to achieve more powerful expressions.

There are three logical operators: ! not && and || orEach logical operator has a truth tableThe truth table for not is: !true false !false true

Page 16: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

16

LOGICAL OPERATROSThe truth table for and is:

true &&true truetrue && false falsefalse && true falsefalse && false false

The truth table for or is:true || true truetrue || false truefalse || true truefalse || false false

Page 17: By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

17

LOGICAL OPERATROSCheck 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