Logical AND and Logical OR Operator:
The AND operator comes in two different flavors in java as does the OR operator.
&& is the Logical AND which operators on boolean operands - Checking if a given condition is true or false.
Likewise || is the Logical OR operator , which operates on boolean operands - Checking if a
condition is true or false.
NOTE: & is a bit-wise operator working at the bit level.
program:
public class MainRunner {
public static void main(String[] args) {
int myScore = 75;
int topper = 98;
if (myScore < 40) {
System.out.println("i got pass marks = " + myScore);
}
if (myScore < topper) {
System.out.println("I have less marks than Topper ");
}
if ((myScore > 40) || (topper > 40)) {
System.out.println(" Either or we both got pas marks");
}
}
}
Homework:
Diff between Assignment operator and Equal to operator:
Program:
public class AssignmentVSEqualto {
public static void main(String[] args) {
int num = 225;
if (num = 24) {
System.out.println("Aggignment operator ");
}
if (num == 25) {
System.out.println("Equal to operator");
}
}
}
The AND operator comes in two different flavors in java as does the OR operator.
&& is the Logical AND which operators on boolean operands - Checking if a given condition is true or false.
Likewise || is the Logical OR operator , which operates on boolean operands - Checking if a
condition is true or false.
NOTE: & is a bit-wise operator working at the bit level.
program:
public class MainRunner {
public static void main(String[] args) {
int myScore = 75;
int topper = 98;
if (myScore < 40) {
System.out.println("i got pass marks = " + myScore);
}
if (myScore < topper) {
System.out.println("I have less marks than Topper ");
}
if ((myScore > 40) || (topper > 40)) {
System.out.println(" Either or we both got pas marks");
}
}
}
Homework:
Diff between Assignment operator and Equal to operator:
Program:
public class AssignmentVSEqualto {
public static void main(String[] args) {
int num = 225;
if (num = 24) {
System.out.println("Aggignment operator ");
}
if (num == 25) {
System.out.println("Equal to operator");
}
}
}
NOT operator:
! or NOT is also known as the logical Complement operator.
Program:
public class AssignmentVSEqualto {
public static void main(String[] args) {
int num = 225;
// if (num = 24) {
// System.out.println("Aggignment operator ");
// }
if (num == 25) {
System.out.println("Equal to operator");
}
boolean mychar = false;
if (mychar = true) {
System.out.println(" This code should not be executed....");
}
if (!mychar) {
System.out.println("Not operator .....");
}
}
}
Ternary Operator :
The Ternary operator is a shortcut to assigning one of two values to a variable depending on given condition.
NOTE: it's shortcut of the if-then-else statement.
program:
public class TernaryOperator {
public static void main(String[] args) {
boolean isCar = false;
if (isCar) {
System.out.println(" is car is true...");
}
isCar = true;
boolean wasCar = isCar ? true : false;
if (wasCar) {
System.out.println("Was car is true");
}
}
}
No comments:
Post a Comment