The if Statement

The if statement is used to test a condition and execute a statement or statements based on the condition. The if statement has the following syntax:

if (condition)
    statement       // condition is true

 

In the above example, if condition is true, then statement is executed. You can use {} brackets to group more than one statement. For example:

if (condition)
    {
    statement1
    statement2
    }

 

An else clause can be added to the if statement:

if (condition)
    statement1      // condition is true
else
    statement2      // condition is false