0% found this document useful (0 votes)
26 views12 pages

21bec0249 25 02 2022

The document discusses various Java assignment, comparison, logical and conditional operators through code examples. It covers the use of assignment operators like =, +=, -= etc. Comparison operators like ==, !=, >, < etc. Logical operators like &&, ||, !. Conditional statements like if, else, else if, switch case and loops like for loop.

Uploaded by

aaeyantomar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

21bec0249 25 02 2022

The document discusses various Java assignment, comparison, logical and conditional operators through code examples. It covers the use of assignment operators like =, +=, -= etc. Comparison operators like ==, !=, >, < etc. Logical operators like &&, ||, !. Conditional statements like if, else, else if, switch case and loops like for loop.

Uploaded by

aaeyantomar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA Program for Practice [25/02/2022]

Name :- Rishabh
Reg No. :- 21BEC0249
Assignment Operators
1) =

public class Main {

public static void main(String[] args) {

int x = 5;

System.out.println(x);

2) +=

public class Main {


public static void main(String[] args) {
int x = 5;
x += 3;
System.out.println(x);
}
}

3) -=
public class Main {
public static void main(String[] args) {
int x = 5;
x -= 3;
System.out.println(x);
}
}

4) *=
public class Main {
public static void main(String[] args) {
int x = 5;
x *= 3;
System.out.println(x);
}
}

5) /=
public class Main {
public static void main(String[] args) {
double x = 5;
x /= 3;
System.out.println(x);
}
}
6) %=

public class Main {

public static void main(String[] args) {

int x = 5;

x %= 3;

System.out.println(x);

7) &=
public class Main {
public static void main(String[] args) {
int x = 5;
x &= 3;
System.out.println(x);
}
}

8) |=

public class Main {

public static void main(String[] args) {

int x = 5;

x |= 3;

System.out.println(x);

9) ^=

public class Main {

public static void main(String[] args) {

int x = 5;

x ^= 3;

System.out.println(x);

}
10) >>=

public class Main {

public static void main(String[] args) {

int x = 5;

x >>= 3;

System.out.println(x);

11) <<=
public class Main {
public static void main(String[] args) {
int x = 5;
x <<= 3;
System.out.println(x);
}
}

Comparison Operators
1) ==
public class Main {
public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x == y); // returns false because 5 is not equal to 3
}
}
2) !=

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x != y); // returns true because 5 is not equal to 3

}
}

3)>
public class Main
{
public static void main(String[] args)
{
int x = 5;
int y = 3;
System.out.println(x > y); // returns true because 5 is greater than 3
}

4)<
public class Main {

public static void main(String[] args) {int x = 5;

int y = 3;

System.out.println(x < y); // returns false because 5 is not less than 3

}
5) >=

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x >= y); // returns true because 5 is greater, or equal, to 3

6) <=

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x <= y); // returns false because 5 is neither less than or equal
to 3

}
Logical Operators
1) && [logical AND]

public class Main {

public static void main(String[] args) {

int x = 5;

System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3
AND 5 is less than 10

2) || [logical OR]
public class Main {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 || x < 4); // returns true because one of the conditions
are true (5 is greater than 3, but 5 is not less than 4)
}
}
3) ! [Logical NOT]

public class Main {

public static void main(String[] args) {

int x = 5;

System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to
reverse the result

JAVA Conditions and If statements

 Use if to specify a block of code to be executed, if a specified condition is true


 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

1) If
public class Main {
p static void main(String[] args) {if (20
u > 18) {
b System.out.println("20 is greater than 18"); // obviously
l
i
c

}
}
}
2) Else
public class Main {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

3) Else..if
public class Main {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

4)
Short-hand for if else[ternary Operator]
public class Main {
public static void main(String[] args) {
int time = 20;
String result;
result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);
}
}

5) Switch-case

public class Main {


public static void main(String[] args) {

int day = 4;

switch (day) {

case 1:

System.out.println("Monday");
break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

}
JAVA Loop

1) For loop

public class Main {

public static void main(String[] args) {

for (int i = 0; i < 5; i++) {

System.out.println(i);

**********************************************************************************

You might also like