Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Operators and Expressions Instructor: Mainak Chaudhuri

Similar presentations


Presentation on theme: "1 Operators and Expressions Instructor: Mainak Chaudhuri"— Presentation transcript:

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

2 2 Announcements Tuesday is our independence day –It is a holy day –The tutorial will meet on Saturday 0800- 0900 –The Tuesday lab will meet on Saturday 1000- 1300 Wednesday is also a holiday –No lab and no class on Wednesday

3 3 Agenda Your first programs in Java Arithmetic operators Expressions

4 4 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"); }

5 5 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”); }

6 6 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); }

7 7 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: >, =, <=, ==, !=

8 8 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: -=, *=, /=, --

9 9 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); }

10 10 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); }

11 11 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!]


Download ppt "1 Operators and Expressions Instructor: Mainak Chaudhuri"

Similar presentations


Ads by Google