HelloUniversity Icon HelloUniversity

Java Control Structures: Mastering Conditional Statements

Published on: August 27, 2025 by Henson M. Sagorsor



Java Control Structures: Mastering Conditional Statements

Why Conditional Statements Matter in Java

“Programs must be written for people to read, and only incidentally for machines to execute.” – Harold Abelson


Every useful program makes decisions. In fact, nearly 80% of all code paths in real-world applications depend on control structures. Without them, software would simply run instructions in a straight line, ignoring the countless possibilities of user input or system state. That would make even the simplest app almost useless!


This is where decision control structures in Java come in. They give your code flexibility. With conditional statements in Java, your program can evaluate conditions, branch into the right action, and ignore paths that don’t apply. Whether it’s checking login credentials, validating input, or assigning grades, these statements are the backbone of decision-making in Java.


In this lesson, you’ll learn exactly how Java handles decision-making through:

  • Relational operators – to compare values
  • Logical operators – to combine or reverse conditions
  • if, if-else, and if-else-if statements – the core conditional structures

By the end, you’ll be able to write code that doesn’t just run — it responds, adapts, and makes decisions just like you do. That’s the real power of learning Java control structures.



Relational Operators in Java

To make decisions, programs need to compare values. Relational operators in Java let you test relationships between numbers, characters, or even variables. Each comparison produces a result of either true or false. These results form the foundation of conditional statements.


List of Relational Operators

Operator Meaning Example Result
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 3 <= 5 true
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true

Example: Using Relational Operators

int x = 10;
int y = 20;

System.out.println(x > y);   // false
System.out.println(x < y);   // true
System.out.println(x == y);  // false
System.out.println(x != y);  // true
            

In this example, Java compares the values of x and y. The program prints out boolean results based on the relational operators used. These results are often the conditions you’ll pass into an if or if-else statement.


Logical Operators in Java

While relational operators in Java compare two values, logical operators let you combine or reverse conditions. They’re essential in writing meaningful conditional statements because real-world decisions often depend on more than one condition at a time.


List of Logical Operators

Operator Meaning Example Result
&& Logical AND (age >= 18 && hasID) true only if both are true
|| Logical OR (age >= 18 || hasID) true if at least one condition is true
! Logical NOT !(hasID) reverses the result

Example: Combining Conditions

int age = 20;
boolean hasID = true;

if (age >= 18 && hasID) {
    System.out.println("Access granted.");
} else {
    System.out.println("Access denied.");
}
            

In this example, the program checks two conditions: whether the user is at least 18 and whether they have an ID. Both conditions must be true for access to be granted. If either one fails, the else block runs.


Why Logical Operators Matter

Think about a login system. You may want to check that a user entered both a correct username and the correct password. Or maybe you want to allow access if they’re an admin or if they have a valid subscription. Logical operators make these kinds of checks possible.


The if Statement in Java

The if statement in Java is the simplest type of conditional. It checks whether a condition is true. If it is, the program runs the code inside the block. If not, the program skips that block and continues.


Structure of an if Statement

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

Example: Age Check

int age = 20;

if (age >= 18) {
    System.out.println("You can register to vote.");
}
            

In this example, the program checks whether age is greater than or equal to 18. Since the condition is true, the message is printed. If the value of age was less than 18, nothing would happen.


Key Points to Remember

  • An if statement only runs its block if the condition is true.
  • If the condition is false, the program moves on without running the block.
  • Conditions are usually written using relational operators and logical operators.

Where You’ll Use if Statements

You’ll use an if statement whenever you want to perform an action only if something is true. Examples include:

  • Checking if a user is logged in before showing their dashboard.
  • Verifying if a file exists before opening it.
  • Confirming if there are enough credits before making a purchase.

The if-else Statement in Java

The if-else statement in Java adds an alternative path. If the condition is true, the program runs the if block. If the condition is false, the program runs the else block instead.


Structure of an if-else Statement

if (condition) {
    // code if condition is true
} else {
    // code if condition is false
}
            

Example: Even or Odd Number

int number = 7;

if (number % 2 == 0) {
    System.out.println("Even number");
} else {
    System.out.println("Odd number");
}
            

In this example, the program checks if number is divisible by 2. If it is, the if block runs and prints "Even number." Otherwise, the else block runs and prints "Odd number."


Key Points to Remember

  • An if-else statement always runs exactly one block — either the if or the else.
  • Use it when you need to handle two opposite outcomes.
  • It’s the most common way to write binary decisions in Java programs.

Where You’ll Use if-else Statements

You’ll use an if-else whenever you need to choose between two possible paths. Examples include:

  • Deciding if a user’s password is correct or incorrect.
  • Checking if a student passed or failed based on their score.
  • Verifying if there’s enough balance in an account to complete a withdrawal.

The if-else-if Ladder in Java

The if-else-if ladder in Java is used when you need to test more than two conditions. The program evaluates each condition in order from top to bottom. As soon as one condition is true, its block runs, and the rest are skipped. If none are true, the else block executes.


Structure of an if-else-if Ladder

if (condition1) {
    // code for condition1
} else if (condition2) {
    // code for condition2
} else if (condition3) {
    // code for condition3
} else {
    // code if none of the conditions are true
}
            

Example: Grading System

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 {
    System.out.println("Grade D or F");
}
            

In this example, the program checks the value of score. It matches the first condition that is true and executes that block. Since score is 85, the program prints "Grade B" and skips the remaining checks.


Key Points to Remember

  • The if-else-if ladder allows testing multiple conditions in sequence.
  • Only one block of code will run — the first one where the condition is true.
  • If no conditions are true, the else block (if provided) runs.

Where You’ll Use if-else-if Ladders

You’ll use an if-else-if ladder whenever you need to handle several possible outcomes. Examples include:

  • Assigning grades based on score ranges.
  • Displaying different shipping fees depending on order value.
  • Checking access levels for users (admin, editor, viewer).

Comparison of Conditional Statements in Java

Java provides different types of conditional statements, and each serves a specific purpose. Here’s a quick comparison to help you decide which one to use:


Statement When to Use Number of Paths Example Use Case
if To test a single condition 1 (either runs or skips) Check if a user is logged in
if-else To choose between two outcomes 2 (true or false) Check if a number is even or odd
if-else-if To test multiple conditions in order Many (first true condition runs) Assign a grade based on score

Key Takeaway

Use if for one condition, if-else for two options, and if-else-if when you have multiple possible outcomes. Choosing the right structure makes your code easier to read and maintain.


Additional Learning Resources

To help you review and practice this lesson on Decision Control Structures in Java, here are some resources you can use:


Expand Your Knowledge

Dive deeper into technology and productivity with these related articles:






We'd Like to Hear Your Feedback

Comments

No comments yet. Be the first to share your thoughts!