java-easyfixtechnology.com

If else statement java

We use an if else statement for decision making in Java programming language. This function helps to execute a particular set of codes based on certain conditions.

The if statement condition is true to run the if body code block and if statement condition is false then run the else body code block.

Syntax

if (condition) {
    //condition is true execute this block
}
else {
    //condition is false execute this block
}

Example 1 : Write a programme for find number is odd or even number?

public class Solution {
    public static void main(String args[]) {
        int number = 10;
        if(number%2==1) {
            System.out.println("Odd number");
        }
        else {
            System.out.println("Even number");
        }
    }
}

OUTPUT:

Even number

Example 2: The ABC institute decided maths exam pass mark is 40. Write a program for check students is pass the exam by using if-else function?

public class Solution {
    public static void main(String args[]) {
       int studentMark = 50;
       if (number >= 40) {
           System.out.println("Student is passed the exam");
       }
       else {
           System.out.println("Student is fail the exam");
       }
    }
}

OUTPUT:

Student is passed the exam

Java if-else-if ladder Statement

When we have multiple statements to check in our program we can use the if-else-if ladder statement.

Syntax

if (condition 1) {
    //condition 1 is true execute this block
}
else if (condition 2) {
    //condition 2 is true execute this block
}
else if (condition 3) {
    //condition 3 is true execute this block
}
...
else {
    //code are execute all conditions are false
}

Example: Write a program for classroom students grading by maths exam marks?

100>mark >= 75 – A
75 > mark >= 60 – B
60 > mark >= 45 – C
45 > mark >= 30 – D

else prints Grade ‘Fail’

public class Solution {
    public static void main(String args[]) {
        int studentMark = 58;
        if(marks<30){  
            System.out.println("Fail");  
        }
        else if(marks>=30 && marks<45){  
            System.out.println("D grade");  
        }
        else if(marks>=45 && marks<60){  
            System.out.println("C grade");  
        }
        else if(marks>=60 && marks<75){  
            System.out.println("B grade");  
        }
        else if(marks>=75 && marks<100){  
            System.out.println("A grade");  
        }
        else{  
            System.out.println("Invalid!");  
        }
    }
}

OUTPUT:

C grade

Java is a popular programming language and widely-used programming language for many years. You can learn java programming with examples by following our Java articles sequentially.

 699 total views,  2 views today

2 thoughts on “If else statement java”

  1. Harleena singh

    I learned if-else statement in java by your blog post. Examples are help to understand quickly.
    Can I use nested if function in java programming?
    Thanks!

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon