1 / 9

Announcements

Announcements. B7 tutorial today 1100-1200 in CS103 In CSE department Ask at entry desk for direction to CS103. Unary and binary operators. Operators acting on a single variable or an expression are called unary operators Example: logical NOT, unary plus, unary minus boolean x = true;

tdeforest
Download Presentation

Announcements

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. Announcements • B7 tutorial today 1100-1200 in CS103 • In CSE department • Ask at entry desk for direction to CS103

  2. Unary and binary operators • Operators acting on a single variable or an expression are called unary operators • Example: logical NOT, unary plus, unary minus boolean x = true; boolean y = !x; // unary logical NOT int z = 23; int w = +z; // unary plus int k = -z; // unary minus or negation int j = -z-20; // what is j: -43 or -3? int r = -(z-20); // what is r?

  3. Unary and binary operators • Operators acting on two variables or expressions are called binary operators • Example: add (+), subtract (-), multiply (*), … • Example of boolean operators (unary and binary) boolean x = true; boolean y = !x && x; boolean z = !(y || x); boolean w = !y && !x; boolean k = !(y && x); boolean p = !y || !x;

  4. Examples Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

  5. Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade); // prints A System.out.println((int)grade); // prints 65 System.out.println(grade+’B’); // prints 131 System.out.println(grade+1); // prints 66 System.out.println((char)(grade+1)); // prints B } } • Notice the type cast • Notice the automatic type conversion

  6. Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade++);// prints A System.out.println(grade); // prints B } } • Post-increment

  7. Printing characters class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(++grade);// prints B System.out.println(grade); // prints B } } • Pre-increment

  8. More type cast class castExample{ public static void main(String arg[]){ double x=Math.PI; // Call to Math class double y=-Math.PI; System.out.println(“Value of PI is: ” + x); System.out.println((int)x); // prints 3 System.out.println((int)y); // prints -3 } }

  9. Celsius to Fahrenheit class CToF{ public static void main(String arg[]){ double C = 40; double F; F = (C/5.0)*9.0 + 32; System.out.println(C + “Celsius is same as ” + F + “ Fahrenheit”); } }

More Related