R: if...else statement

In if-statement we can see that if the condition is true it will execute the set of statements and if a condition is false it won't. But if we want to do something else we can use the else statement with if.

So if the condition is false it will execute the set of statements under else. The else part is only executed when the condition is false.  

Syntax

if (condition)
{
    #set of statement if the condition is True
} else
{
    #set of statement if the condition is False
}

Flowchart

R if else statement

Example

a<-3

#check the condition
if(a%%2==0)
{ 
    print("Even Number") 
}else
{ 
    print("Odd Number") 
}

Output: [1] "Odd Number"