Download presentation
Presentation is loading. Please wait.
Published byJeffry Glenn Modified over 9 years ago
1
By: Mr. Baha Hanene Chapter 6
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) 2
3
CONTENTS Basic Arithmetic Operators Operator Precedence Typecasting Increment & Decrement Operators Relational Operators Logical Operators 3
4
BASIC ARITHEMATIC OPERATORS OperatorsDescription +Used for Addition Operation -Used for Subtraction Operation *Used for Multiplication Operation /Used for Division Operation %Used for Remainder Operations 4
5
OPERATOR PRESEDENCE OperatorsPrecedence ( )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. 5 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 6
7
7 x = (2.1 + num) / 2 * 5Ans. : 0.51 x = (2.1 + num) / (2 * 5)Ans. : 12.75 x = num / 2Ans. : 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
8
COMPARE THE FOLLOWIG Exp. 8 Suppose x=2 & y=4 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 9 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. 10 If b is a float variable b = (float) 10 / 3 Ans. 3.333333 b = 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.
11
INCREMENT & DECREMENT OPERATORS OperatorsDescription ++aadd one to (a) before using it a++add one to (a) after using it --asubtract one from (a) before using it a--subtract one from (a) after using it 11 ++a “pre-increment” a++ “post-increment” --a “pre-decrement” a-- “post-decrement”
12
12
13
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
14
RELATIONAL OPERATORS The following table explains how the relational operators are evaluated: (3 == 3)trueResult will be 1 (3 != 3)falseResult will be 0 (3 > 3)falseResult will be 0 (3 >= 3)trueResult will be 1 (3 < 3)falseResult will be 0 (3 <=3)trueResult 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
15
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: !truefalse !falsetrue 15
16
LOGICAL OPERATROS The truth table for and is: true &&truetrue true && falsefalse false && truefalse false && falsefalse The truth table for or is: true || truetrue true || falsetrue false || truetrue false || falsefalse 16
17
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*/ 17 Note: All logical operations will either result in 1 or 0 i.e. True or False
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.