Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – The if statement (if without else statement)

It is possible to use if statement without the else statement.

Syntax

if(condition)
{
    // group of
    // programming
    // statements
}

If the condition inside parentheses is true, if-block is executed. After execution of if-block, program control executes statements after it.

If the condition inside the parenthesis is false, if-block is skipped, and statements after it are executed.

Flowchart of if control statement

Java Programming if statement Flowchart

Working

Java Programming Language if statement Working

Example No. 1

class IfDemo1 
{
    public static void main(String args[])
    {
        int i;
        i = 5;
        if(i<10)
        {
            System.out.println("Inside if block");
        }
        System.out.println("After if block");
    }
}

Output

Inside if block
After if block

In the above program value of i is less than 10. Therefore condition becomes true and program control enters into if-block and executes the statement inside it.

Example No. 2

class IfDemo2 
{
    public static void main(String args[])
    {
        int i;
        i = 15;
        if(i<10)
        {
            System.out.println("Inside if block");
        }
        System.out.println("After if block");
    }
}

Output

After if block

In the above program value of i is greater than 10. Therefore condition becomes false and program control skips if-block and executes the statement after it.

 

Leave a reply