Understanding If Condition in Java

Understanding If Condition in Java

4 mins read23 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on May 9, 2024 17:04 IST

Have you ever wondered how Java programs make decisions? The if condition in Java is the key. It allows code to branch out based on conditions, thus introducing dynamic decision-making into applications. Let us understand it more!

In Java, an if statement is used to test a condition and execute a code block if the condition is true. It's a fundamental part of controlling the flow of a Java program, allowing it to make decisions and execute different actions based on various conditions.

Let us see the working of the if condition in Java, starting with syntax, flowchart, and explanation.

Must read If-Else Program in Java 

1. Syntax

if (condition) {
    // Block of code to be executed if the condition is true
}

2. Flowchart

3. Explanation

  • An if statement starts with the keyword if followed by a condition in parentheses.
  • The condition is a Boolean expression (an expression that evaluates to true or false).
  • If the condition is true, the code block inside the if statement executes.
  • If it's false, the code block is skipped.
 

Examples of if condition in Java

Let's see a few examples to understand the concept deeply!

Example 1: Checking for a Passing Grade

Problem Statement: Determine if a student has passed a course. The passing grade is 50 or above.


 
public class GradeCheck {
public static void main(String[] args) {
int grade = 65; // Student's grade
if (grade > = 50) {
System.out.println("Congratulations! You have passed.");
}
}
}
Copy code

Output

Congratulations! You have passed.

If the student's grade is 50 or higher, the message "Congratulations! You have passed." is printed. In this case, since the grade is 65, which is above 50, the output will be "Congratulations! You have passed."

Example 2: Checking for Leap Year

Problem Statement: Check if a year is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.


 
public class LeapYearCheck {
public static void main(String[] args) {
int year = 2020; // Year to check
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
}
}
}
Copy code

Output

2020 is a leap year.

The condition checks if the year is divisible by 4 and not by 100, or it is divisible by 400. For the year 2020, it satisfies the first condition (divisible by 4 and not by 100), so it prints "2020 is a leap year."

Example 3: Checking for Eligibility to Vote

Problem Statement: Determine if a person is eligible to vote. The eligibility age for voting is 18 years or older.


 
public class VotingEligibilityCheck {
public static void main(String[] args) {
int age = 17; // Person's age
if (age > = 18) {
System.out.println("You are eligible to vote.");
}
}
}
Copy code

No Output

The condition checks if the person's age is 18 or more. Since the age is 17, which is less than 18, the condition is false, and nothing is printed. The person is not eligible to vote.

Java Comments | About, Types and Examples
Java Comments | About, Types and Examples
Do you know what makes the code more readable? Comments, as they provide valuable context and explanations about the code, making it easier for both the original developers and others...read more

Star Pattern Programs in Java
Star Pattern Programs in Java
Pattern programs in Java are a type of problem that uses nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

A Guide to Java Math Class
A Guide to Java Math Class
Have you ever wondered how mathematical computations are handled in Java? The Java Math class is a powerhouse of static methods and constants that facilitate a wide range of mathematical...read more

Key Takeaways

  • The if statement in Java is crucial for implementing decision-making logic and executing code blocks based on Boolean conditions.

  • Its straightforward syntax enhances code readability and maintainability.

  • It is foundational for constructing complex conditional structures like if-else and nested if statements.

FAQs

What is the "if" condition in Java?

The "if" condition in Java is a fundamental control flow statement that allows a program to execute a block of code based on whether a particular condition evaluates to true. It's the simplest form of decision-making in Java, guiding the program to make choices and execute specific actions accordingly.

How do you write a basic "if" statement in Java?

A basic "if" statement in Java can be written as follows:

if (condition) {
    // Block of code to be executed if the condition is true
}
Here, condition is any expression that returns a boolean value (true or false). If the condition is true, the code inside the curly braces {} is executed.

Can you combine multiple conditions in an "if" statement in Java?

Yes, you can combine multiple conditions in an "if" statement using logical operators such as && (logical AND), || (logical OR), and ! (logical NOT). For example:

if (condition1 && condition2) {
    // Block of code to be executed if both condition1 and condition2 are true
}

What is an "if-else" statement, and how does it differ from an "if" statement?

An "if-else" statement in Java allows you to execute one block of code if the condition is true and another block if the condition is false. It extends the "if" statement by providing a fallback action when the initial condition doesn't hold:

if (condition) {
    // Block of code to be executed if the condition is true
} else {
    // Block of code to be executed if the condition is false
}

The difference is that an "if" statement only specifies what to do if a condition is true, whereas an "if-else" statement covers both possibilities (true and false).

How does an "if-else-if" ladder work in Java?

An "if-else-if" ladder in Java is used when you have multiple conditions to check sequentially. Once a true condition is found, its corresponding block of code is executed, and the rest of the ladder is skipped:

if (condition1) {
    // Block of code to be executed if condition1 is true
} else if (condition2) {
    // Block of code to be executed if condition1 is false and condition2 is true
} else {
    // Block of code to be executed if all previous conditions are false
}

This structure allows for complex decision-making by checking multiple conditions in a specified order.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio