Java If Statement | If-Else | Nested If-Else | Conditional Statements with Examples

 As we know, there are various applications of programming languages in various fields. There are cases when, we want something to happen only when a certain condition is there, like, for example: pour beer into the glass until the bottle is not empty. In such cases, Conditional statements come into play, where the control or the flow of execution is to be determined by a set of conditions, we use statements like If, If-Else, If-else-if, switch, break, and continue.  


In the upper illustration, our condition would be - until the beer bottle is empty. Till then we have to execute - pouring beer in glass. See, its that simple.

Even the various Operators in Java, can be used in condition making, like we have to execute something, until condition is true or false. This is known as Boolean testing.


If Statement

If statement is used to specify that a following block of code will be executed, when the condition is true.

Syntax:
if(condition)
{
Statements....
}

Example:
if(3<5)
{
System.out.print("Programmer Studios");
}

* Here the given block of statements only execute when the condition is true. Also if we have only one statement in the If-block then we can skip the curly brackets. If automatically executes the next first statement. And if the condition if false, the block of statements is skipped.

Program:

public class ifStatement {

    public static void main(String[] args) {
        int a=1;
        int b=12;
        System.out.println("Beginning the program");

        if(a<b) {
            System.out.println(a+" is smaller than "+b);
            System.out.println("First If block executed");
        }

        if(a>b) {
            System.out.println(a+" is greater than "+b);
            System.out.println("Second If block executed");
        }
        System.out.println("Last Statement executed");
    }
}

OUTPUT:
Beginning the program
1 is smaller than 12
First If block executed
Last Statement executed


If-else Statement

It is also similar to the If statement, but it also has a else block to be executed, if the condition in If-block gets false.

Syntax:
if(Condition){
Statements....
}
else{
Statements....
}

Example:
if(3>5){
System.out.println("3 is greater than 5");
}
else{
System.out.println("3 is lesser than 5");
}

Program:
public class ifElseStatement {
    public static void main(String[] args) {
        int a,b;
        a=b=13;

        if(a==b) {
            System.out.println("a is equal to b");
        }
        else {
            System.out.println("a is not equal to b");
        }

    }
}

OUTPUT:
a is equal to b

If-else-if Statement

Here we provide multiple conditions, that means, if one condition is true, execute a articular code, else if another condition is true, execute that particular code, else execute another.

Syntax:
if(condition){
Statements..
}
else if(condition)
{
Statements...
}
else{
Statements..
}

Example:
if(10>5)
{
System.out.println("First condition is true");
}
else if(10==9+1)
{
System.out.println("Second condition is true");
}
else{
System.out.println("First condition is false");
}

Here if the condition-1 is true, then its block will we executed and if it is false, it would be skipped,  otherwise control will go to second condition and similarly if it is true, then it would be executed. This way the control goes from one if-statement to another.

Program:
public class ifElseIfStatement {
    public static void main(String[] args) {
        int age=23;
        if(age<13) {
            System.out.println("Child");
        }
        else if(age>13 && age<18) {
            System.out.println("Teenager");
        }
        else if(age>18 && age<23) {
            System.out.println("Youngster");
        }
        else if(age>23 && age<40) {
            System.out.println("Adult");
        }
        else {
        System.out.println("Old");
        }

    }
}

OUTPUT:
Old

Nested If-else Statements

Nested refers to using a conditional block in another conditional block, for example: if condition in another if condition. This is nesting, similar concepts are followed in Loops and other concepts.

Syntax:
if(condition-1){
if(condition-2){
Statements....
}
else{
Statements....
}
}
else{
Statements....
}

Program:
public class largestOf3NumbersUsingNestedIfElse {
    public static void main(String[] args) {
        int a,b,c;
        a=12;
        b=13;
        c=14;
        if(a>b) {
            if(a>c) {
                System.out.println(a+" is greatest");
            }
            else {
                System.out.println(c+" is greatest");
            }
        }
        else {
            if(b>c) {
                System.out.println(b+" is greatest");
            }
            else {
                System.out.println(c+" is greatest");
            }
        }
    }
}

OUTPUT:
14 is greatest

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post