Java Programming Lesson 09 Mr Muhammad Hanif Lecturer
- Slides: 16
Java Programming Lesson 09 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh 1
Operators 2
Operators �Arithmetic Operators �Relational Operators �Logical Operators �Bitwise Operators 3
Arithmetic Operators Symbol + – * / % += –= *= /= %= ++ –– Operator Addition Subtraction (also unary minus) Multiplication Division Modulus Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment Increment Decrement 4
Arithmetic Operators (Integer) package javaapplication 1; public class L 9_Int_Opr { Program: Write a program in java using addition, subtraction, multiplication, negative and remainder operators, use integer datatype. public static void main(String[] args) { int var_add=1+4; int var_sub=var_add-2; int var_mul=var_sub*2; int var_div=var_mul/2; int var_rem=var_div%var_add; int var_neg=-var_rem; System. out. println("add="+var_add+" sub="+var_sub+" mul="+var_mul); System. out. println("div="+var_div+" rem="+var_rem+" neg="+var_neg); } } 5
Arithmetic Operators (Integer) package javaapplication 1; public class L 9_dble_Opr { Program: Write a program in java using addition, subtraction, multiplication, negative and remainder operators, use double datatype. public static void main(String[] arg) { double var_add=1+4; double var_sub=var_add-2; double var_mul=var_sub*2; double var_div=var_mul/2; double var_rem=var_div%var_add; double var_neg=-var_rem; System. out. println("add="+var_add+" sub="+var_sub+" mul="+var_mul); System. out. println("div="+var_div+" rem="+var_rem+" neg="+var_neg); } } 6
Arithmetic Operators (Arithmetic Assignment) �Arithmetic Operators are used in combination with assignment operator. �Example 1: � int a=a+4; � int a+=4; �Example 2: � int a=a%4; � int a%=4; Assignment: Use all arithmetic Operators with assignment operator, with the help of for loop. 7
Arithmetic Operators (Increment/Decrement) package javaapplication 1; public class L 9_inc_dec Program: Write a program demonstrating pre-increment and post-increment. { public static void main(String[] arg) { int var_pre_inc=5; int var_post_inc=10; System. out. println("pre increment: "+(++var_pre_inc)); System. out. println("pre increment: "+var_post_inc++); } } Output: pre increment: 6 pre increment: 7 pre increment: 10 pre increment: 11 Assignment: Write a program demonstrating pre-decrement and post decrement operators.
Assignment Operator �Used to assign value at right side of equal sign to the variable placed at left side of variable. �a=5; �It can be used to assign values to chain of variables. �E. g. � a=b=c=d=100; // set a, b and c as 100. 9
Ternary Operator �It is used to replace if-then-else statement. �Syntax: �expression 1? expression 2: expression 3 �If expression 1 is true, expression 2 is evaluated otherwise expression 3 is evaluated. 10
Arithmetic Operators (Increment/Decrement) package javaapplication 1; public class L 9_ternary Program: Write a program demonstrating ternary operator with the help of integer and String data types { public static void main(String[] arg) { int batch=5; int fees=(batch>1&&batch<=5)? (2500): (5000); System. out. println("Batch: "+batch+" fees="+fees); int marks=60; String status=marks>40? ("Pass"): ("Fail"); System. out. println("Status is: "+status); } } Output: Batch: 5 fees=2500 Status is: Pass
Arithmetic Operators (Increment/Decrement) package newpackage; public class ex_pre_post { public static void main(String[] arg) { int a=-6; int b=(a>0)? (1): (0); System. out. println(b); Program: Write another program demonstrating ternary operator with the help of AND operator. Assignment: Write a program demonstrating ternary operator, also use OR operator. int z=-5; String x=(z<0)? ("Positive"): ("Nagative"); System. out. println(x); int c=70; String d=(c>50)? ("Greater than 50"): ("Less than 50"); System. out. println(d); int marks=40; String status=(marks>0&&marks<40)? ("Fail"): ("Pass"); System. out. println("Your Status is: "+ status); } } Output: 0 Positive Greater than 50 Your Status is: Pass
Relational Operators �Relational Operators are used to compare operands and create a relation between them. �Following are relational operators: �Equal to (==) �Not equal to (!=) �Less than (<) �Greater than (>) �Less than or equal to (<=) �Greater than or equal to (>=) 13
Relational Operators Operator Description (A=10, B=20) Example == Checks if the values of two operands are equal or not, if yes then (A == B) is not condition becomes true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the (A >= B) is not value of right operand, if yes then condition becomes true <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true. 14
Relational Operators package javaapplication 1; Program: Write a program which describe the use of all relational operators. public class L 9_Relational { public static void main(String[] args) { int value 1 = 10; } } int value 2 = 20; if(value 1 == value 2) System. out. println("value 1 == value 2"); if(value 1 != value 2) System. out. println("value 1 != value 2"); if(value 1 > value 2) System. out. println("value 1 > value 2"); if(value 1 < value 2) System. out. println("value 1 < value 2"); if(value 1 >= value 2) System. out. println("value 1 >= value 2"); if(value 1 <= value 2) System. out. println("value 1 <= value 2"); Output value 1 != value 2 value 1 <= value 2 15
Relational Operators package javaapplication 1; public class L 9_Relational Program: Write a program which describe the use of all relational operators and logical operators. { public static void main(String[] args) { int a=40, b=20, c=50, d=100; if(a==b||b==c||c==d) System. out. println("a==b||b==c||c==d"); Output a==b||b==c||c==d a<b||b<c||c<d a>b||b>c||(c>d) a<=b||b<=c||c<=d a>=b||b>=c||c>=d if(a!=b||b!=c||c!=d) System. out. println("a!=b||b!=c||c!=d"); if(a<b||b<c||c<d) System. out. println("a<b||b<c||c<d"); if(a>b||b>c||(c>d)) System. out. println("a>b||b>c||(c>d)"); if(a<=b||b<=c||(c<=d)) System. out. println("a<=b||b<=c||c<=d"); if(a>=b||b>=c||(c>=d)) System. out. println("a>=b||b>=c||(c>=d)"); } } Assignment: Write a program which uses all relational operators and also use AND operator. 16
- Lecturer's name or lecturer name
- Contoh proses transformasi
- Hanif nur widhiyanti
- Hanif nur widhiyanti
- Dr asif hanif
- Hanif khalak
- اا
- At tahiyyatu
- Jeannie watkins
- Spe distinguished lecturer
- Good afternoon teachers
- Photography lecturer
- Lecturer in charge
- Designation lecturer
- Designation of lecturer
- Why himalayan rivers are pernnial in nature
- Lecturer name