Full Transcript

ARITHMETIC, LOGICAL, AND RELATIONAL OPERATIONS Learning Objectives: Perform arithmetic, logical, and relational operations. Write expressions using operators. JAVA OPERATORS are symbols that perform operations on variables and values. ARITHMETIC OPERATORS ...

ARITHMETIC, LOGICAL, AND RELATIONAL OPERATIONS Learning Objectives: Perform arithmetic, logical, and relational operations. Write expressions using operators. JAVA OPERATORS are symbols that perform operations on variables and values. ARITHMETIC OPERATORS ARITHMETIC OPERATORS OPERATOR OPERATION are used to + Addition Subtraction perform - Multiplication arithmetic * Division operations on / Modulo variables and % Operation data. ARITHMETIC Ex. OPERATORS int x, y; int sum = x + y; //addition operation int diff = x – y; //subtraction operation int prod = x * y; //multiplication operation int quot = x / y; //division operation int mod = x % y; //modulo operation RELATIONAL OPERATORS RELATIONAL OPERATORS OPERATOR DESCRIPTION are used to == Is Equal To check the != Is Not Equal To relationship < Less Than between two > Greater Than operands. = Greater Than or decision making Equal To and loops. RELATIONAL Ex. OPERATORS int x = 5; int y = 8; if(x > y){ System.out.println(x+” is greater than ”+y); } else if (x y) && (x > z)); // false // || operator System.out.println((x < y) || (x > z)); // true // ! operator System.out.println(!(y == z)); // true ASSIGNMENT OPERATORS ASSIGNMENT = (assignment) OPERATORS += (addition assignment) -= (subtraction assignment) *= (multiplication assignment) /= (division assignment) %= (modulo assignment) Example public class JavaOperators { public static void main(String[] args) { // Arithmetic operators // Assignment operators System.out.println("\nAssignment operators:"); int x = 15; x += 5; // Equivalent to x = x + 5 System.out.println("x: " + x); x -= 3; // Equivalent to x = x – 3 System.out.println("x: " + x); x *= 2; // Equivalent to x = x * 2 x /= 4; // Equivalent to x = x / 4 x %= 7; // Equivalent to x = x % 7 System.out.println("x: " + x); } Unary OPERATORS UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 1. Increment and Decrement Operators: ++ (increment): Increases the value of the operand by 1. Prefix form: ++x (increments x before using its value) Postfix form: x++ (uses the current value of x and then increments it) -- (decrement): Decreases the value of the operand by 1. Prefix form: --x (decrements x before using its value) Postfix form: x-- (uses the current value of x and then decrements it) Example public class JavaOperators { public static void main(String[] args) { int x = 5; int y = ++x; // y becomes 6, x becomes 6 int z = x++; // z becomes 6, x becomes 7 } } UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 2. Unary Plus and Minus: + (unary plus): Returns the value of the operand unchanged. - (unary minus): Negates the value of the operand. Example public class Main { public static void main(String[] args) { int a = 10; int b = -a; // b becomes - 10 int c = +a; System.out.println(a); System.out.println(b); System.out.println(c); UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 3. Bitwise Operator: used to perform operations on individual bits of a number. They are often used in low-level programming, system programming, and cryptography. 3.1. Bitwise AND (&) Operation: Performs a logical AND operation on each corresponding bit of two operands. Result: If both bits are 1, the result is 1. Otherwise, it's 0. Example: A = 1010 (binary) B = 0101 (binary) A & B = 0000 (binary) UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 3.2. Bitwise OR (|) Operation: Performs a logical OR operation on each corresponding bit of two operands. Result: If at least one bit is 1, the result is 1. Otherwise, it's 0. Example: A = 1010 (binary) B = 0101 (binary) A | B = 1111 (binary) UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 3.3. Bitwise NOT (~) Operation: Inverts all bits of an operand. Result: 1 becomes 0, and 0 becomes 1. Example: A = 1010 (binary) ~A = 0101 (binary) UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 3.4. Bitwise XOR (^) Operation: Performs a logical XOR (exclusive OR) operation on each corresponding bit of two operands. Result: If the bits are different, the result is 1. Otherwise, it's 0. Example: A = 1010 (binary) B = 0101 (binary) A ^ B = 1111 (binary) UNARY OPERATORS Unary operators operate on a single operand. They are used to perform various operations on variables or expressions. 3.5. Left Shift (> 2 = 0010 (binary) (logical shift) Example public class Main { public static void main(String[] args) { int c = 5; // binary representation: 00000101 int d = ~c; // binary representation: 11111010 (decimal value: -6) System.out.println(c+d); } } THANK YOU! REFERENCES: https://www.programiz.com/java-progra mming/operators https://www.w3schools.com/java/java_op erators.asp

Use Quizgecko on...
Browser
Browser