Pages

Saturday, December 25, 2021

Decision Statements - if

Concept 

The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a boolean expression is true.

Syntax

Here is the general format of the if statement:

    if (BooleanExpression)
    statement;

Logic



How if statement works?

The if condition that appears inside the parentheses must be a boolean expression. A boolean expression may be either true or false. If it is true, the very next statement is executed. Otherwise, it is skipped. The statement is conditionally executed because it executes only under the condition that the expression in the parentheses is true.


Example (IfDemo.java)

class IfDemo {
    public static void main(String args[])
    {
        int i = 10;
  
        if (i < 15)
            System.out.println("10 is less than 15");
  
            System.out.println("Outside if-block");
    }
}

Output
10 is less than 15
Outside if-block




No comments:

Post a Comment