WikiGalaxy

Personalize

Java If Else

Basic If Statement:

The if statement is a fundamental control structure that allows you to execute a block of code only if a specified condition is true. It is often used to make decisions in your code.


      int number = 10;
      if (number > 5) {
          System.out.println("Number is greater than 5");
      }
    

Console Output:

Number is greater than 5

If-Else Statement

If-Else Structure:

The if-else statement allows you to choose between two blocks of code. If the condition is true, the first block executes; otherwise, the second block runs.


      int number = 3;
      if (number > 5) {
          System.out.println("Number is greater than 5");
      } else {
          System.out.println("Number is 5 or less");
      }
    

Console Output:

Number is 5 or less

If-Else If-Else Chain

Multiple Conditions:

The if-else if-else chain allows you to test multiple conditions. The code block for the first true condition will execute.


      int number = 10;
      if (number < 5) {
          System.out.println("Number is less than 5");
      } else if (number == 5) {
          System.out.println("Number is 5");
      } else {
          System.out.println("Number is greater than 5");
      }
    

Console Output:

Number is greater than 5

Nested If Statements

Nested Conditions:

You can nest if statements to check multiple conditions. This is useful for more complex decision-making processes.


      int number = 8;
      if (number > 5) {
          if (number < 10) {
              System.out.println("Number is between 6 and 9");
          }
      }
    

Console Output:

Number is between 6 and 9

Using Logical Operators

Combining Conditions:

Logical operators such as && (AND) and || (OR) allow you to combine multiple conditions in a single if statement.


      int age = 25;
      if (age > 18 && age < 30) {
          System.out.println("Age is between 19 and 29");
      }
    

Console Output:

Age is between 19 and 29

If with Ternary Operator

Ternary Operator:

The ternary operator is a shorthand form of the if-else statement. It is useful for simple conditions and assignments.


      int number = 7;
      String result = (number % 2 == 0) ? "Even" : "Odd";
      System.out.println(result);
    

Console Output:

Odd

If with Strings

String Comparison:

When comparing strings in Java, use the .equals() method instead of == to ensure correct comparison.


      String name = "Alice";
      if (name.equals("Alice")) {
          System.out.println("Hello, Alice!");
      }
    

Console Output:

Hello, Alice!

Handling Null with If

Null Check:

Always check for null before calling methods on objects to prevent NullPointerException.


      String text = null;
      if (text != null && text.equals("Hello")) {
          System.out.println("Text is Hello");
      } else {
          System.out.println("Text is not Hello or is null");
      }
    

Console Output:

Text is not Hello or is null

Switch vs. If-Else

When to Use:

While if-else is versatile, switch statements can be more efficient for multiple exact matches. However, switch does not handle ranges or complex conditions.


      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("Other day");
      }
    

Console Output:

Wednesday

If with Boolean Expressions

Boolean Logic:

Boolean expressions are often used directly in if statements to simplify code logic.


      boolean isActive = true;
      if (isActive) {
          System.out.println("The system is active");
      }
    

Console Output:

The system is active

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025