1 / 4

Operators and Expressions: Wrap up

Operators and Expressions: Wrap up. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Operator precedence. Higher to lower precedence ++, -- (both post and pre) unary !, +, - (logical NOT, unary +, unary -) unary *, /, % (multiply, divide, mod) binary

jonesfrank
Download Presentation

Operators and Expressions: Wrap up

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operators and Expressions: Wrap up Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

  2. Operator precedence • Higher to lower precedence ++, -- (both post and pre) unary !, +, - (logical NOT, unary +, unary -) unary *, /, % (multiply, divide, mod) binary +, - (add, subtract) binary <, <=, >, >= (LT, LE, GT, GE) binary ==, != (equality and non-equality) binary && (logical AND) binary || (logical OR) binary = (assignment) binary

  3. Operator precedence • What is the answer? (assume x=1) x*x+x%2 2*x/2%4 (need to know associativity) • Almost all operators are left associative • This indicates in which direction the operators of the same precedence will evaluate • Assignment has right associativity x=y=2; // assigns y=2 first and then x=y • The above expression would lead to wrong answer if assignment was left associative

  4. Default initial values • Every declared variable gets a pre-defined default initial value in Java • Not true for many other languages • All primitive types get a zero value (boolean gets false) • You should not rely on this in your programs even if the compiler allows you to do so • Never use uninitialized variables in a program

More Related