All the Operators 13-Apr-19.

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

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.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
Chapter 4: Basic C Operators
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.
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Java Operator Precedence Operator Meaning Assoc. ===========================================================.
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.
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.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
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.
Rational Expressions relational operators logical operators order of precedence.
Operator Kinds of Operator Precedence of Operator Type Casting.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
C++ Programming Language Lecture 4 C++ Basics – Part II
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Operators And Expressions
Operators and Expressions
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Selections Java.
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Operators and Expressions
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.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions.
Data Types and Expressions
All the Operators 6-Apr-19.
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Chapter 12 Variables and Operators
Presentation transcript:

All the Operators 13-Apr-19

Precedence An operator with higher precedence is done earlier (precedes) 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: 3 + 4 * 5 gives 23 10 - 5 - 2 gives 3 Exceptions: unary operators, casts, assignment operators, the ternary operator: all done right to left

. Accesses methods and variables 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 Frequently the lowest precedence operator is assignment, for example x = y + z;

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

Additive operators + Add - Subtract

<< Left shift, end off 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

<= Less than or equal to > Greater than Relational operators < Less than <= Less than or equal to > Greater than >= Greater than or equal to instanceof Determines whether its left operand is an object whose type (class or interface) is the right operand Example: if (myPet instanceof Dog) {...} These all have the same precedence, and it is higher than equality/inequality tests

A beginner’s error if (0 <= i < a.length) { ... } Operations are done left to right 0 <= i will be either true or false Neither true < a.length nor false < a.length is legal The correct expression should be if (0 <= i && i < a.length) { ... }

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

^ XOR Exclusive OR There is no ^^ operation 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