Operators and Expressions Instructor Mainak Chaudhuri mainakccse iitk

  • Slides: 10
Download presentation
Operators and Expressions Instructor: Mainak Chaudhuri mainakc@cse. iitk. ac. in 1

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

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

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

Printing on monitor /* Example of multiline comment; This is our first Program */

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 ESc 101 N"); } } 3

Printing on monitor /* Example of multiline comment; This is our second Program. Will

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 ” + “ESc 101 N”); } } 4

Printing numbers class printnumber { public static void main (String args[]){ // Notice that

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 var 1; // Declaration var 1 = 124; // Operator and expression System. out. println("Value of var 1 is: "+var 1); } } 5

Operators • Arithmetic operators +, -, *, /, % – Addition: a+b – Subtraction:

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

Expressions • An expression is a statement involving operators and variables or constants Example:

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

Expressions • More examples class Some. Thing { public static void main (String args[])

Expressions • More examples class Some. Thing { 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); } 8 }

Expressions • More examples class Some. Thing { public static void main (String args[])

Expressions • More examples class Some. Thing { 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); } 9 }

Logical operators • Not: ! • AND: && • OR: || Examples: int x

Logical operators • Not: ! • AND: && • OR: || Examples: int x = 12; boolean is. Two. Digit = ((x >= 10) && (x < 100)); boolean is. Multiple. Of. Six = ((x%6)==0); boolean is. Odd = !((x%2)==0); boolean is. Less. Than 100 Perfect = ((x==6) || (x==28)); boolean is. Two. Digit = (10 <= x < 100); [Wrong!] 10