If else statement in java

An if…..else statement is a type of control flow statement in which there are two blocks of code. The first block of code, located inside the if statement, will be executed if the condition specified in the statement evaluates to true. If the condition is false, the second block of code, located inside the else statement, will be executed instead.

The syntax for an if…..else statement can be written as follows:

if (condition) {
	//block of code
} else {
	//block of code
}

Example:

public class JavaIf {
    public static void main(String[] args) {
        String name = "Mohan";
        int Roll = 25;
        if (name == "Mohan" && Roll == 26) {
            System.out.println("Details of Mohan.");
        } else {
            System.out.println("Invalid details.");
        }
    }
}

Output:

Invalid details.
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in