If Else Statement

What is the if else statement in Java?

If-else statements are conditional statements used in computer program execution to follow pre-defined rules. They allow you to execute a specific block of code if a condition is true; otherwise, it checks other conditions. If-else statements are essential for controlling the flow of a program and establishing rules.

If the condition specified in the “if-statement” is false, then the code block inside the “else statement” will be executed.

Syntax:

if (condition) {

// Code block to be executed if the condition is true

}

else{

// Code block to be executed if the condition is false

}

Explanation of this Syntax:
  • The “if” keyword is used to start the conditional statement, followed by a condition inside the parentheses. The condition should be an expression that evaluates to a boolean value (true or false).
  • If the condition specified in the “if” statement is true, the code block inside the first set of curly braces will be executed. This block contains the code that should run if the condition is true.
  • If the condition is false, the code block inside the “else” block (the second set of curly braces) will be executed. This block contains the code that should run if the condition is false.

The “if-else” statement allows you to choose between two different blocks of code based on the outcome of the condition. If the condition is true, the code inside the “if” block is executed; otherwise, the code inside the “else” block is executed.

 

 

Java if  else Statement Example

Example 1:
package com.example.myapp; // Package name

public class Main { // Class name

    public static void main(String[] args) {
        int n1 = 5;

        if (n1 % 2 == 0)
            System.out.println("even");
        else
            System.out.println("odd");
    }
}
Output:
odd
Explanation of this code:
  1. int n1=5; :- This line declares an integer variable named “n1” and initializes it with the value 5.
  2. if(n1%2==0): This is an “if” statement that checks if the value of “n1” is even. The % operator is used to calculate the remainder when “n1” is divided by 2. If the remainder is 0, it means “n1” is even.
  3. System.out.println(“even”); :- If the condition in the “if” statement is true (i.e., “n1” is even), this line will be executed, and it will printeven” to the console.
  4. else: If the condition in the “if” statement is false (i.e., “n1” is not even), the program will move to the “else” block.
  5. System.out.println(“odd”); :- The code inside the “else” block will be executed, and it will printodd” to the console. This line will be executed when “n1” is not even, indicating that it is an odd number.

In summary, the code checks if the value of the variable “n1” is even or odd using an “if-else” statement and prints the corresponding message accordingly. If “n1” is even, it will printeven,” and if “n1” is odd, it will printodd.”

Example 2:
package com.example.taxcalculator; // Package name

public class IfElseExample {
    public static void main(String[] args) {
        //Requirement:
        // if age is greater then 63 then print "NO TAX" and print "10% Discount"
        // unless print "TAX WILL APPLY" and print "0% Discount"
        int age;
        age = 20;

        if (age > 63) {
            System.out.println("NO TAX");
            System.out.println("10% Discount");
        } else {
            System.out.println("TAX WILL APPLY");
            System.out.println("0% Discount");
        }
    }
}
Output:
TAX WILL APPLY

0% Discount
Explanation of this code:

The code snippet represents a scenario where the age of an individual is being evaluated to determine tax and discount eligibility. Here’s the explanation:

  1. A variable named “age” is declared and assigned a value of 20, representing the age of an individual.
  2. The code uses an “if-else” statement to check whether the age is greater than 63.
  3. If the age is greater than 63:
    • The program displays “NO TAX,” indicating that the individual is exempt from paying taxes.
    • It also displays “10% Discount,” indicating that the individual is eligible for a discount of 10%.
  4. If the age is 63 or below:
    • The program displays “TAX WILL APPLY,” indicating that the individual needs to pay taxes.
    • It also displays “0% Discount,” indicating that the individual is not eligible for any discount.

In summary, the code determines whether an individual qualifies for tax exemption and a discount based on their age. If the age is greater than 63, there will be no tax, and a 10% discount will apply. Otherwise, if the age is 63 or below, taxes will be applicable, and there will be no discount.

Visited 1 times, 1 visit(s) today

Comments are closed.

Close