Monday, July 21, 2025

Java Learn If

# Java If Condition - Full Guide

The `if` statement is one of the most fundamental control flow statements in Java. It allows your program to execute certain code blocks conditionally. Here's a complete guide to Java's `if` condition.

## Basic If Statement

```java
if (condition) {
    // code to execute if condition is true
}
```

Example:
```java
int age = 20;
if (age >= 18) {
    System.out.println("You are an adult.");
}
```

## If-Else Statement

```java
if (condition) {
    // code if true
} else {
    // code if false
}
```

Example:
```java
int score = 75;
if (score >= 50) {
    System.out.println("Passed!");
} else {
    System.out.println("Failed.");
}
```

## If-Else If-Else Statement

```java
if (condition1) {
    // code if condition1 is true
} else if (condition2) {
    // code if condition2 is true
} else {
    // code if all conditions are false
}
```

Example:
```java
int grade = 85;
if (grade >= 90) {
    System.out.println("A");
} else if (grade >= 80) {
    System.out.println("B");
} else if (grade >= 70) {
    System.out.println("C");
} else {
    System.out.println("D or below");
}
```

## Nested If Statements

```java
if (condition1) {
    if (condition2) {
        // code if both conditions are true
    }
}
```

Example:
```java
int age = 25;
boolean hasLicense = true;

if (age >= 18) {
    if (hasLicense) {
        System.out.println("You can drive!");
    } else {
        System.out.println("You need a license to drive.");
    }
} else {
    System.out.println("You're too young to drive.");
}
```

## Conditional Operators

Common operators used in if conditions:

- **Relational operators**: `==`, `!=`, `>`, `<`, `>=`, `<=`
- **Logical operators**: `&&` (AND), `||` (OR), `!` (NOT)

Example:
```java
int temperature = 25;
boolean isSunny = true;

if (temperature > 20 && isSunny) {
    System.out.println("Great weather for a picnic!");
}

if (temperature <= 0 || temperature >= 40) {
    System.out.println("Extreme weather condition!");
}
```

## Ternary Operator (Shorthand If-Else)

```java
variable = (condition) ? expressionTrue : expressionFalse;
```

Example:
```java
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);
```

## Common Mistakes to Avoid

1. Using assignment `=` instead of equality `==`:
   ```java
   if (x = 5) { }  // Wrong! This assigns 5 to x
   if (x == 5) { } // Correct
   ```

2. Forgetting curly braces (though optional for single statements):
   ```java
   if (condition)
       statement1;  // Only this is part of if
       statement2;  // Always executes
   ```

3. Overly complex conditions that could be simplified:
   ```java
   // Instead of:
   if (x > 5 && x < 10 || x == 15 && !(y < 2))
   
   // Consider breaking into multiple conditions
   ```

## Best Practices

1. Keep conditions simple and readable
2. Use parentheses to clarify operator precedence
3. Consider using switch statements for multiple fixed values
4. For object comparison, use `.equals()` instead of `==` (unless comparing primitives)

Would you like me to explain any specific aspect of Java's if conditions in more detail?

No comments: