R If Else Statement

If statement

It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if statement’.

Syntax:

First of all, it will check the given expression or test condition, if it is true then all the statements within the block (statement 1, 2…. n) will be executed, else all those statements will be skipped.

Flow Chart:

R if else

 

 

 

 

 

 

 

 

 

 

 

 

Example:

Output:

if…else statement

If the condition is true, then if the block is executed. But if the condition is false, then else block is executed.

Syntax:

Flow Chart:

R if else statement

 

 

 

 

 

 

 

 

 

 

 

 

Example:

Output:

if…else if…else statement

It is an extension of if…statement. If the condition is true, then the statement of if block will be executed, else another if…else statement is evaluated. You may append any number of an if…else statement followed by one to another.

Syntax:

Flow Chart:

R if-else-if

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:

Output: