Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

Types and Arithmetic Operators
Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
CHAPTER 8 OPERATORS AND EXPRESSIONS IN C++. OPERATORS The operations (specific task) are represented by operators and the objects of the operation(s)
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
OPERATORS.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
Operators.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Operators and Expressions
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Prepared By: K. U. Khimani Assistant Professor IT Department.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Variables, Operators, and Expressions
Operators & Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Type casting Algorithm & flowchart
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Chapter 3 Operators and Expressions
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
OPERATORS in C Programming
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Operators and Expressions

Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==, !=) 3.Logical (&&, ||, !) 4.Assignment (=, +=, -=, *=, /=, %=) 5.Increment(decrement) (++, --) 6.Conditional(? :) 7.Bitwise(&, |, ^, >) 8.Special(,,sizeof)

Arithmetic operators Examples OperatorMeaning +Addition or unary plus -Subtraction or unary minus *Multiplication /Division %Modulo division (applied only to integers)

Arithmetic operators In arithmetic, the remainder is the amount "left over" after the division of two integers which cannot be expressed with an integer quotient. The general form of a linear equation can be expressed as a = q * d + r. In this equation, q can be referred to as the quotient and d as the divisor, while r as the remainder. The equation can be transformed to find the remainder as: r = a - q * d. For ex:a = 810, q = 3, d = 256, r = 42 But we only know a and d and we want to find q and r. a / d =  q =  a/d  = 3 and r = 810 – 3 * 256 = 42

Arithmetic operators: Example Decimal to octal number system conversion Mixed-mode arithmetic 15/10.0 = /10 = 1

Arithmetic operators: Example

The program converts a given number of days into months and days

Relational operators We often compare two quantities and depending on their relation, take certain decisions. For example you have 1000 Won in your pocket, then when the question is: Do I have enough money to buy an ice cream In C this question can be written as follows YES (TRUE) generally any non-zero value NO (FALSE)

Relational operators OperatorMeaning <Is less than <=Is less than or equal to >Is greater than >=Is greater than or equal to ==Is equal to !=Is not equal to A simple relational expression contains only one relational operator: arithm_expr1 relat_op arithm_expr2 4.5 <= 10 TRUE 4.5 < -10 FALSE -35 >= 0 FALSE 10 < 7+5 TRUE a + b == c + d TRUE // if a + b is equal to c + d Relational operators are used in decision statements such as if and while to decide the course of action of a running program.

Relational operators: Complements Among the six relational operators, each is a complement of another operator. >is complement of<= <is complement of>= ==is complement of!= We can simplify an expression involving the not and the less than operator using the complements as shown below: Actual oneSimplified one !(x<y)x => y !(x>y)x <= y !(x!=y)x == y !(x<=y)x > y !(x>=y)x < y !(x==y)x != y

Logical operators I an addition to the relational operators, C has the following three logical operators. &&Meaning logicalAND ||Meaning logicalOR !Meaning logicalNOT They are used when we need to test more than one conditions to make decisions. An example is: a > b && x == 10 The compound expression is true when a > b and x == 10 is true.

Logical operators Relative precedence of the relational and logical operators is as follows op-1 op-2 Value of the expression op-1 && op-2op-1 || op-2 Non-zero Highest priority! > >= < <= == != && Lowest priority ||

Assignment operators We have already applied the usual assignment operator. In addition, C has a set of shorthand assignment operators of the formv op= expr; is equivalent to v = v op (expr) Statement with simple assignment operator Statement with shorthand operator a = a + 1a += 1 a = a -1a -= 1 a = a * (n+1)a =* (n+1) a = a / (n+1)a =/ (n+1) a = a % ba %= b

Assignment operators The use of shorthand assignment operators has advantages: What appears on the left-hand side need not be repeated and therefore it becomes easier to write. The statement is more concise and easier to read. The statement is more efficient. value[5*j-2] = value[5*j-2] + delta; value[5*j-2]+= delta;

Increment and decrement operators C allows two very useful operators not generally found in other languages. There are the increment and decrement operators: ++ and – The operator ++ adds 1 to the operand, while – subtracts 1. ++m; is equivalent to m = m + 1; (or m+= 1) --m; is equivalent to m = m -1; (or m-= 1) m = 5; y = ++m; m = 5; y = m++; Prefix operator Postfix operator m is 6 and y is 5 m is 6 and y is 6 Example: m = n++ - j + 10; Old value of n is used in evaluation.

Conditional Operator A ternary operator pair “? :” is available in C to construct conditional expressions of the form: exp1 ? exp2 : exp3 where exp1, exp2, and exp3 are expressions. exp1 is a condition and is evaluated first. If it is true, then the expression exp2 is evaluated and becomes the value of expression. Otherwise exp3 becomes the value of expression. a = 10; b = 15; x = (a > b) ? a : b;

Bitwise operator Bitwise operators are used for testing the bits, or shifting them right or left. Bitwise operators are not applied to float or double. OperatorMeaning &Bitwise AND |Bitwise OR ^Bitwise exclusive OR <<Shift left >>Shift right inputs& 0 & 00 0 & 10 1 & 00 1 & 11 inputs| 0 & 00 0 & 11 1 & 01 1 & 11 inputs^ 0 & 00 0 & 11 1 & 01 1 & 10

Special operators C supports some special operators of interest such as comma operator, value = (x = 10, y = 5, x + y); while (c = getchar(), c != ‘10’) t = x, x =y, y = t; sizeof operator m = sizeof(sum); n = (long int); K = sizeof(235L) pointer operators (& and *) and member selection operators (. and ->)

Summary example

Arithmetic expressions Algebraic expressionC expression a · b - ca * b - c (m+n) ·(x+y)(m+n) * (x+y) A*b/c 3*x*x + 2 * x + 1 x/y + c

Evaluation of expressions Expressions are evaluated using an assignment statement of the form variable = expression;

Precedence of arithmetic operators An arithmetic expression without parentheses will be evaluated from left to right using the rules of precedence of operators. There are two priority levels: High priority * / % Low priority + - The basic evaluation procedure includes ‘two’ left-to-right passes. x = a – b/3 + 2*c – 1, when a = 9, b = 12, and c =3, x = 9 – 12/3 + 3*2 - 1 First pass 1.x = 9 – * x = 9 – Second pass 1.x = x = 11 – 1 3.x = 10

Precedence of arithmetic operators 9 – 12/3 + 3* (1) (2) (3) (4) (5) Plot similar diagram for /(3 + 3)*(2-1). Consider that ( ) has the highest priority!

Type conversions in expressions C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance (implicit type conversion). int i,x; float f; double d; long int l; x = 1 / I + i * f - d long int float int double In an expression with mixed type the result will be converted to the type that represents wider range; All short and char are automatically converted to int;

Type conversions in expressions shortchar int unsigned int long int unsigned long int float double long double Conversion hierarchy

Type conversions in expressions Sometimes we want to force a type conversion in a way that is different from the automatic conversion. Example: float ratio; int female_number = 7; int male_number = 5; ratio = female_number / male_number; // the ratio will be 1 ratio = (float ) female_number / male_number; // the ratio is 1. The operator (float) converts the female_number to floating point for the purpose of evaluation of the expression. The general form of a cast (explicit conversion) is (type-name)expression

Type conversions in expressions Casting can be used to round-off a given value. Consider the following statement: x = (int) (y + 0.5); If y is 27.6, y+0.5 is 28.1 and on casting, the result becomes 28, the value that is assigned to x. ExampleAction x = (int) is converted to integer by truncation. a = (int) 21.3 / (int) 4.5Evaluated as 21/4 and result would be 5. b = (double) sum/nDivision is done in floating mode. y = (int) (a + b)The result of a + b is converted to integer z = (int) a + ba is converted to integer and the added to b p = cos((double) x)Converts x to double before using it.

Operator precedence and associativity Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; Category Operator Associativity Postfix () [] -> Left to right Unary + - ! ~ (type) * & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift > Left to right Relational >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>> = <<= &= ^= |= Right to left Comma, Left to right

Mathematical functions Trigonometric double acos(double x) Compute arc cosine of x. double asin(double x)Compute arc sine of x. double atan(double x)Compute arc tangent of x. double atan2(double y, double x)Compute arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value. double cos(double x)Compute cosine of angle in radians. double sin(double x)Compute sine of angle in radians. double tan(double x)Compute tangent of angle in radians. Hyperbolic functions double sinh(double x)Compute the hyperbolic sine of x. double cosh(double x)Compute the hyperbolic cosine of x. double tanh(double x)Compute the hyperbolic tangent of x. Other functions double sqrt(double x)Compute the square root of x. double exp(double x)Compute exponential of x double fabs (double x )Compute absolute value of x. double floor(double x)Get largest integral value less than x. double ceil(double x) Get smallest integral value that exceeds x. double log(double x)Natural log of, x > 0 double pow (double x, double y)Compute x raised to the power y. double frexp(double x, int *expptr)Breaks down x into mantissa and exponent of no. void srand(unsigned seed)Set a new seed for the random number generator (rand). labs(long n) Find absolute value of long integer n. #include