If Statement

What is the if statement in Java?

The “if” statement in Java enables condition-based decision-making, which results in a Boolean value, and then executing specific code based on the condition’s truth. The primary idea is to run the code block if the condition is true. There are different types of decision-making statements.

Below are the four types of ‘if-else’ statements in the Java programming language:
  1. If statement
  2. If-else statement
  3. If-else-if ladder
  4. Nested if statement
Syntax:

if([variable][comparison operator][value]) {

// If the condition is true, execute this code block within curly braces in Java-like syntax.

}

Explanation:
  1. The “if” keyword initiates the condition.
  2. The condition is enclosed within parentheses ( ).
  3. If the condition evaluates to true, the code inside the curly braces { } will be executed.
  4. If the condition is false, the code block inside the curly braces { } will be skipped, and no action will be taken.

Java if Statement Example

Example 1:
package com.example.demo;

public class DayOfWeekDemo {
    public static void main(String[] args) {

// if day value is 1 then print Monday 
// if day value is 2 then print Tuesday 
// if day value is 3 then print Wednesday 
// if day value is 4 then print Thursday 
// if day value is 5 then print Friday 
// if day value is 6 then print Saturday 
// if day value is 7 then print Sunday

        int dayValue;
        dayValue = 3; // here = sign is called the assignment operator
                      // == sign is called the Equal sign
        System.out.println("Current Day value is " + dayValue);

        if (dayValue == 1)
            System.out.println("Monday");
        if (dayValue == 2)
            System.out.println("Tuesday");
        if (dayValue == 3)
            System.out.println("Wednesday");
        if (dayValue == 4)
            System.out.println("Thursday");
        if (dayValue == 5)
            System.out.println("Friday");
        if (dayValue == 6)
            System.out.println("Saturday");
        if (dayValue == 7)
            System.out.println("Sunday");
    }
}
Output:
Current Day value is 3 Wednesday
Explanation of this code:

This code snippet checks the value of the variable “dayValue” and prints the corresponding day of the week:

  • If “dayValue” is 1, it prints “Monday.”
  • If “dayValue” is 2, it prints “Tuesday.”
  • If “dayValue” is 3, it prints “Wednesday.”
  • If “dayValue” is 4, it prints “Thursday.”
  • If “dayValue” is 5, it prints “Friday.”
  • If “dayValue” is 6, it prints “Saturday.”
  • If “dayValue” is 7, it prints “Sunday.”

It uses individual “if” statements for each day, which means each condition is checked independently. There is no “else” statement, so if “dayValue” doesn’t match any of the specified values, nothing will be printed.

Example 2:
package com.example.demo;

public class NumberComparison {
    public static void main(String[] args) {
//If any number is greater than 10 then print "This number is greater than 10". 
// If any number is <= 10 then print "This number is less than or equal to 10".

        int num = 10;
        
        if (num > 10)
            System.out.println("This number is greater than 10");
        if (num <= 10)
            System.out.println("This number is less than or equal to 10");
    }
}
Output:
This number is less than or equal to 10
Explanation of this code:
  • The code first assigns the value 10 to the variable “num.”
  • The firstif” statement checks if the value of “num” is greater than 10, but in this case, “num” is equal to 10 (not greater), so the condition evaluates to false.
  • The code inside the first “if” block will be skipped since the condition is false, and no message will be printed for that “if” block.
  • The secondif” statement checks if the value of “num” is less than or equal to 10. Since “num” is equal to 10, this condition evaluates to true.
  • The code inside the second “if” block will be executed, and the message “This number is less than or equal to 10” will be printed to the console.

The final output of this program will be “This number is less than or equal to 10” as “num” is equal to 10.

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close