1 / 10

Operators and Expressions

Operators and Expressions. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Agenda. Your first programs in Java Arithmetic operators Expressions. Printing on monitor. /* Example of multiline comment; This is our first Program */ class printcoursename{

Download Presentation

Operators and Expressions

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 Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

  2. Agenda • Your first programs in Java • Arithmetic operators • Expressions

  3. Printing on monitor /* Example of multiline comment; This is our first Program */ class printcoursename{ public static void main(String arg[]){ // This prints name of the course (single line comment) System.out.println("This course is ESc101N"); } }

  4. Printing on monitor /* Example of multiline comment; This is our second Program. Will print the same thing in a different way. */ class printcoursename{ public static void main(String arg[]){ // This prints name of the course (single line comment) System.out.println("This ” + “course ” + “is ” + “ESc101N”); } }

  5. Printing numbers class printnumber { public static void main (String args[]){ // Notice that I can use args also // because it is a variable name and can be // anything. int var1; // Declaration var1 = 124; // Operator and expression System.out.println("Value of var1 is: "+var1); } }

  6. Operators • Arithmetic operators +, -, *, /, % • Addition: a+b • Subtraction: a-b • Multiplication: a*b • Division: a/b (what is this? Find out in lab) • Remainder: a%b • Assignment operator = • Comparison operators: > , < , >= , <=, == , !=

  7. Expressions • An expression is a statement involving operators and variables or constants Example: x = a + b; // Add a and b and put the // result in x Example: int x = 6; // Declaration and // initialization int y = x; y = y + 1; // Same as y += 1; // Same as y++; • Two new operators: += , ++ • More operators: -= , *= , /= , --

  8. Expressions • More examples class SomeThing { public static void main (String args[]) { int x, y; boolean z, w; x = 10; y = 12; z = (x > y); w = (y >= x); System.out.println(“z: ” + z + “,” + “w: ” +w); } }

  9. Expressions • More examples class SomeThing { public static void main (String args[]) { int x, y; boolean z, w; x = 10; y = 10; z = (x == y); w = (y != x); System.out.println(“z: ” + z + “,” + “w: ” +w); } }

  10. Logical operators • Not: ! • AND: && • OR: || Examples: int x = 12; boolean isTwoDigit = ((x >= 10) && (x < 100)); boolean isMultipleOfSix = ((x%6)==0); boolean isOdd = !((x%2)==0); boolean isLessThan100Perfect = ((x==6) || (x==28)); boolean isTwoDigit = (10 <= x < 100); [Wrong!]

More Related