- The operators == and != (not equal) can be used to compare values. They return true or false boolean values.
- One = sign changes the value of a variable. Two == equal signs are used to test if a variable holds a certain value, without changing its value.
- In Java, relational operators like <, >, <=, >=, ==, and != are used to compare numeric values, while string comparisons are done using compareTo and equals.
- Example:
int a = 5;
int b = 10;
if (a == b) {
System.out.println("a and b are equal");
} else {
System.out.println("a and b are not equal");
}
- You can use mod (%) to check for odd or even numbers. If num % 2 == 0, num is even; otherwise, it’s odd.
- Example:
int num = 4;
if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
- Statements in a Java main method execute in top-to-bottom order. If statements (conditionals) alter the flow, executing code only if a condition is true.
- A common mistake in if statements is using = instead of == in the condition by mistake. Always use == for comparisons.
- Example:
int x = 10;
if (x == 10) {
System.out.println("x is 10");
} else {
System.out.println("x is not 10");
}
- Use if-else statements to execute one block of code if a condition is true, and another block if false.
- Example:
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
- In nested if statements, an else clause pairs with the closest unmatched if statement.
- Example of nested if:
int num = 5;
if (num > 0) {
if (num % 2 == 0) {
System.out.println("Positive even number");
} else {
System.out.println("Positive odd number");
}
}
- The switch statement offers another way to handle 3 or more conditional cases.
- Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
- Use && as a logical AND, and || as a logical OR to join two Boolean expressions.
- Example using &&:
boolean isAdult = true;
boolean hasTicket = true;
if (isAdult && hasTicket) {
System.out.println("Access granted");
} else {
System.out.println("Access denied");
}
- Example using ||:
boolean isWeekend = true;
boolean isHoliday = false;
if (isWeekend || isHoliday) {
System.out.println("Relaxing day");
} else {
System.out.println("Work day");
}
- Short-circuit evaluation means if one part of a complex condition is sufficient to determine the result, the rest is ignored.
- De Morgan’s Laws in Java: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b.
- Example:
boolean a = true;
boolean b = false;
if (!(a && b)) {
System.out.println("Not both are true");
} else {
System.out.println("Both are true");
}
- Negating an expression with relational operators flips the operator: !(c < d) becomes c >= d.
- Example:
int c = 5;
int d = 10;
if (!(c < d)) {
System.out.println("c is greater than or equal to d");
} else {
System.out.println("c is less than d");
}
- The equals method compares two strings letter by letter. Use equals instead of == to check for String equality.
- Example:
String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are not equal");
}
- == compares object references, not values. Use == to compare objects to null to check existence.
- Example:
String str = null;
if (str == null) {
System.out.println("str is null");
}
- Common mistakes include using = instead of == in if conditions, ending if statements with a semicolon, and using two ifs instead of an if-else.
- Example of common mistake with a semicolon:
if (x == 10); {
System.out.println("x is 10"); // Wrong: extra semicolon causes logical error
}
- Example of using == instead of =:
if (x = 10) { // Wrong: use == for comparison
System.out.println("x is 10");
}