on Leave a Comment

Java if else Statement

In a java program, there may be a scenario when we have to execute a section of code, if a particular condition is satisfied. This can be done by if statement. Java has various types of if statements,

1. If statement
2. If-else statement
3. If-else-if ladder
4. Nested if statement

Java if Statement

Syntax of if statement in java:

if(condition)
{
  //statements
}

If the condition is true, if statements will execute. And if the condition is false, if statements will skip from the execution. 

Example of java if statement:

class JavaIfStatement
{
    public static void main(String [] args)
    {
        int x = 2; 
        if(x%2==0)
        {
            System.out.println(x+" is even number");
        }
    }
}

Output:

2 is even number

In this program, x is equal to 2 and we checks whether it is completely divisible by 2 or not through if statement. If x is completely divisible by 2, if block will execute and prints that x is even number.

Java if-else Statement

Syntax of if-else statement in java:

if(condition)
{
  //statements
}
else
{
  //statements
}

Example of java if-else statement:

class JavaIfElseStatement
{
    public static void main(String [] args)
    {
        int x = 13; 
        if(x%2==0)
        {
            System.out.println(x+" is even number");
        }
        else
        {
            System.out.println(x+" is odd number");
        }
    }
}

Output:

13 is odd number

Here x is equal to 13, if else statement checks whether it is completely divisible by 2 or not. If it is completely divisible by 2, if block executes and prints that x is even number, if x is not completely divisible by 2, else block executes and print that x is odd number.  

Java if-else-if ladder Statement

Syntax of if-else-if ladder statement in java:

if(condition1)
{  
  //statements
}
else if(condition2)
{  
  //statements
}  
...  
else
{  
  //statements
}

Example of java if-else-if ladder statement:

class JavaIfElseIfStatement
{
    public static void main(String [] args)
    {
        int x = 0; 
        if(x<0)
        {
            System.out.println(x+" is negative number");
        }
        else if(x>0)
        {
            System.out.println(x+" is positive number");
        }
        else
        {
            System.out.println(x+" is equal to zero");
        }
    }
}

Output:

0 is equal to zero

This program checks whether x is positive number, negative number or equal to zero through if-else-if ladder. 

Java nested if ladder Statement

Example of Java nested if ladder Statement

class JavaNestedIfStatement
{
    public static void main(String [] args)
    {
        int a = 5, b = 6, c = 7, d = 8; 
        if(a>b || a>c || a>d)
        {
            System.out.println(a+" is greatest number");
        }
        else
        {
            if(b>c)
            {
               if(b>d)
               {
                  System.out.println(b+" is greatest number");
               }
            }
            else if(c>d)
            {
                System.out.println(c+" is greatest number");
            }
            else
            {
                System.out.println(d+" is greatest number");
            }
        }
    }
}

Output:

8 is greatest number

This program checks greatest of 4 numbers using nested if ladder.

0 comments:

Post a Comment