All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C Operators, Operands, Expressions & Statements Hands-on, crash course with code examples 1/46 ©
Chapter 4: Basic C Operators
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
LESSON 6 – Arithmetic Operators
C Operators. CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
CPSC 233 Tutorial Xin Jan 24, Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.
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.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Java Operator Precedence Operator Meaning Assoc. ===========================================================.
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
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Operator Kinds of Operator Precedence of Operator Type Casting.
DEPARTMENT OF COMPUTER SCIENCE C Operators, Operands, Expressions & Statements 1/46 BCA I ST YR (SEMESTER I) PRESENTED BY SHAZIA SHAFI Dated:
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Operators And Expressions
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Arithmetic Operator Operation Example + addition x + y
Chapter 12 Variables and Operators
Chapter 12 Variables and Operators
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
More about Numerical Computation
All the Operators 4-Dec-18.
Chapter-3 Operators.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Chapter 4: Expression and Operator
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Chapter 12 Variables and Operators
Presentation transcript:

All the Operators

Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated with a lower number; zero is the highest precedence Most of the time, operators with equal precedence are done left to right –Examples: * 5 gives gives 3 Exceptions: unary operators, casts, assignment operators, the ternary operator: all done right to left

Postfix operators Postfix operators have the highest precedence ( parameters ) Parameter lists [ ] Brackets indicate indexing into an array. Accesses methods and variables expr ++, expr -- Postincrement, postdecrement

Unary prefix operators Unary prefix operators have the next highest precedence: ++ expr Preincrement -- expr Predecrement + - Unary plus and unary minus ! Logical negation (not) ~ Bitwise complement (invert every bit)

Object creation and casting new Create a new instance of a class ( type ) Cast (convert) to the given type Slides are in order of decreasing precedence –Higher precedence means “more tightly bound” –The lowest precedence operator is the “main” operator in an expression

Multiplicative operators * Multiply / Divide % Modulus These all have the same precedence

Additive operators + Add - Subtract

Shift operators << Left shift, end off –For small integers, this is equivalent to multiplying by a power of two –Example: 100 << 3 gives 800 >> Right shift with sign extension –For small integers, this is equivalent to an integer divide by a power of two –Example: 100 >> 2 gives 25 >>> Right shift with zero fill –Does not make sense for numbers

Relational operators < Less than <= Less than or equal to > Greater than >= Greater than or equal to instanceof Left operand is an object whose type (class or interface) is the right operand These all have the same precedence, and it is higher than equality/inequality tests

Equality and inequality == Test if equal –For primitive types, tests if the values are equal –For objects, tests if both sides refer to the same object != Test if not equal –For primitive types, tests if the values are unequal –For objects, tests if the sides refer to different objects Reminder: these tests should not be used on floating- point numbers ( float or double )

AND & AND –For integral types, ANDs each corresponding pair of bits 0 & 0 == 0 0 & 1 == 0 1 & 0 == 0 1 & 1 == 1 –For booleans, performs the logical AND operation Boolean & is like &&, but both operands are evaluated, even if it is possible to decide the result from the left operand alone

Exclusive OR ^ XOR –For integral types, XORs each corresponding pair of bits 0 ^ 0 == 0 0 ^ 1 == 1 1 ^ 0 == 1 1 ^ 1 == 0 –For booleans, performs the logical XOR operation a ^ b is true if either a is true or b is true, but not both There is no ^^ operation

OR | OR –For integral types, ORs each corresponding pair of bits 0 | 0 == 0 0 | 1 == 1 1 | 0 == 1 1 | 1 == 1 –For booleans, performs the logical OR operation Boolean | is like ||, but both operands are evaluated, even if it is possible to decide the result from the left operand alone

The ternary operator boolean-expr ? expression-1 : expression-2 This is like if-then-else for values rather than for statements If the boolean-expr evaluates to true, the result is expression-1, else it is expression-2 Example: max = a > b ? a : b ; sets the variable max to the larger of a and b expression-1 and expression-2 need not be the same type, but either result must be useable The ternary operator is right associative! –To avoid confusion, use parentheses if your expression has more than one ternary operator

The assignment operators I The assignment operators have the lowest precedence –Assignment is an operation –Assignment is right associative a = b = c = 7.5 * w; assigns 7.5*w to c, then assigns c to b, then assigns b to a – if all these assignments are legal Example: – if ((line = reader.newLine()) == null) {... }

The assignment operators II There are a lot of assignment operations besides = variable += expression means the same as variable = variable + expression variable -= expression means the same as variable = variable - expression variable *= expression means the same as variable = variable * expression variable /= expression means the same as variable = variable / expression

The assignment operators III variable %= expression means the same as variable = variable % expression variable <<= expression means the same as variable = variable << expression variable >>= expression means the same as variable = variable >> expression variable >>>= expression means the same as variable = variable >>> expression

The assignment operators IV variable &= expression means the same as variable = variable & expression variable |= expression means the same as variable = variable | expression variable ^= expression means the same as variable = variable ^ expression

What you need to know You should understand what each operator does Parameter lists, array indexing, casting, postfix ++ and --, and the dot operator are done first –In particular, a cast refers to the one following entity, so to cast the result of an expression you need extra parentheses –Example 1: variable = ( type )( expression ); –Example 2: variable = (( type ) variable ). method (); In arithmetic, the unary operators + and – are done first, then multiplication and division, then addition and subtraction All assignment operators are done last For anything else, it’s a good idea to use parentheses anyway (even if you remember the order of precedence, other people won’t)

The End