0Pricing
Java Academy · Lesson

If Statements – Basics

Write decisions with if, else if, else. Combine comparisons with logical operators and avoid common mistakes.

If Statements – Basics is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Decisions Overview

Decisions with if

Programs often need to choose a path based on data. In Java, if evaluates a boolean expression and executes a block when it is true.

  • Comparison: == != > < >= <=
  • Logical: && (and), || (or), ! (not)
  • Blocks are inside braces { }.

If Syntax

Basic syntax

The condition in parentheses must be true or false.

public class Main {
    public static void main(String[] args) {
        int age = 18;

        if (age >= 18) {
            System.out.println("Adult");
        }
    }
}

If-Else Chain

if–else adds an alternative; else if chains multiple checks.

public class Main {
    public static void main(String[] args) {
        int score = 85;

        if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else {
            System.out.println("Try again");
        }
    }
}

Boolean Operators

Boolean operators

  • && (and): both sides true
  • || (or): at least one true
  • ! (not): flips true/false

Example: if (age >= 18 && country.equals("TR"))

Pitfalls

Common mistakes

  • Using = instead of == in comparisons
  • Forgetting braces, so only one statement is controlled
  • Comparing strings with == instead of equals()

If Demo

Try it: Enter a score and see how the chain selects one branch. The boolean passed is computed with a comparison.

public class Main {
  public static void main(String[] args) {
    // Example score value without Scanner
    int score = 85;

    if (score >= 90) {
      System.out.println("Grade: A");
    } else if (score >= 80) {
      System.out.println("Grade: B");
    } else if (score >= 70) {
      System.out.println("Grade: C");
    } else if (score >= 60) {
      System.out.println("Grade: D");
    } else {
      System.out.println("Grade: F");
    }

    boolean passed = score >= 60;
    System.out.println("Passed: " + passed);
  }
}

Range Check

Quick check: Which condition checks that x is between 10 and 20 (inclusive)?

Recap & Next

Recap: You wrote decisions with if/else, combined conditions with logical operators, and avoided common pitfalls.

Next: Practice with nested conditions and compare strings safely with equals().

Frequently asked questions

Is the “If Statements – Basics” lesson free?

Yes — the full text of “If Statements – Basics” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “If Statements – Basics”?

Write decisions with if, else if, else. Combine comparisons with logical operators and avoid common mistakes. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “If Statements – Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. If Statements – Basics
  2. If Statements – Practice & Nested
  3. Common Pitfalls & Debugging Messages
← Back to Java Academy