i

R Programming Complete Tutorial

R - If…Else Statement

If…Else Statement

The syntax of the if...else statement is almost the same as If statement, only, the difference is, it will take care of False also.

Syntax:

Flow Diagram:

If the condition is true, it will execute statement 1, and in case of FALSE, it will perform the second statement. This flow diagram is exactly telling the same.

Example:

This if-else condition will check whether a number is even or odd.

First, it will take input from the user, then do the modules of two operations and check whether the result is 0 or not. If it is 0, it means there will be no reminder, and the number is even; otherwise, the number is odd.

There is another form of if...else statement, which will be explained in the next section.