JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java If-else Statement

In this tutorial, we'll learn about control flow statements using Java if and if...else statements. We'll learn how to use the if-else statement in Java with the help of examples.

Types of if statement

There are various types of if statement in Java.

1. if statement

2. if-else statement

3. if-else-if statement

4. nested if statement

These statements allow we to control the flow of our program’s execution based upon conditions.

Java if Statement

if statement is the most simple decision making statement. The Java if statement tests the condition. It executes the if block if condition is true otherwise not.

Syntax:

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

Example:

// Java program to illustrate If statement
class Simple{
    public static void main(String args[]){

    // defining an age variable
    int age = 20;

//checking the age
    if(age >= 18){
    System.out.println("You can vote");
    }
    }
}

Output:

You can vote

Example 2: Java if with String

// Java program to illustrate If statement
class Simple{
    public static void main(String args[]){

    // defining crush variable
    String crush = "Ankita";

//if statement
    if(crush == "Ankita"){
    System.out.println("Ankita is your crush");
    }
    }
}

Output:

Ankita is your crush

Java if-else Statement

The java if-statement is also used to perform a task depending on whether a given condition is true or false.

It executes the if block if condition is true otherwise else block is executed.

Syntax:

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

Example:

// Java program to illustrate If-else statement
class Simple{
    public static void main(String args[]){

    int number = 5;

//checks if number is greater than 0
    if(number > 0){
    System.out.println("The number is positive");
    }

//if number is less than 0
// execute this block
    else{
    System.out.println("The number is negative");
    }
    }
}

Output:

The number is positive

Example: Check the number is odd and even

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

    int number = 15;

    //Check if the number is divisible by 2 or not
    if(number % 2 == 0){
        System.out.println("Even number");
    }

    //if the number is not divisible by 2
    // execute this block
    else{
        System.out.println("Odd number");
    }
    }
}

Output:

Odd number

Java if-else-if Statement

In Java, we have an if...else...if ladder, that can be used to execute one condition from multiple statements.

Syntax:

if(condition1){
// if condition1 is true
// code to be executed
}
else if(condition2){
// if condition2 is true
// code to be executed
}
else if(condition3){
// if condition3 is true
// code to be executed
}
...
else{
// if all conditions are false
// code to be executed
}

Example:

// Java program to illustrate If-else-if statement
// Java program to check a number is positive, negative or zero.

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

    int number = 15;

    if(number > 0){
    System.out.println("Positive");
    }
    else if(number < 0){
    System.out.println("Negative");
    }
    else{
    System.out.println("Zero");
    }
    }
}

Output:

Positive

Java Nested if statement

In java the nested if statement represents the if block within another if block. if the outer if block condition is true, then the inner if block condition executes.

Syntax:

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

Example:

// Java program to illustrate If-else-if statement

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

    String username = "rj801";
    int password = 12345;

    if(username == "rj801"){
        if(password == 12345){
        System.out.println("Login sucessful");
        }
        else{
        System.out.println("incorrect password");
        }
    else{
    System.out.println("incorrect username");
    }
    }
}

Output:

Login sucessful