In the real programming world, the R If Statement is the primary decision-making statement. R If Statement tests the condition first, and depending upon the result, executes the statements. If the test condition is true, then only statements within the if block executed.
R If Statement Syntax
The syntax of the If statement in R Programming language has a simple structure
if (Boolean_Expression) { Statement 1; Statement 2; …………. …………. Statement n; }
From the above code snippet, If the Boolean_Expression / test condition inside the If statement is true, then the statements (Statement 1, Statement 2,……., Statement n) executed. Otherwise, all those statements skipped.
R If Statement Flow Chart
The below picture shows the flow chart behind the If Statement in R Programming language
- If the test condition is true, STATEMENT 1 executed, followed by STATEMENT N.
- If the condition is False, STATEMENT N executed because it placed outside of the if condition block, and it has nothing to do with the condition result. Let us see one example to understand better.
R If Statement example
The R if statement program allows the user to enter any positive integer, and it checks whether the user-specified number is Positive or Not using the if statement.
# R IF Statement Example number <- as.integer(readline(prompt="Please Enter any integer Value: ")) if (number > 1) { print("You have entered POSITIVE Number") }
NOTE: If statement in R Programming does not require the curly brackets to hold a single statement. But, It is always good practice to use curly brackets.
From the screenshot below, you can see that we entered 25 as a number. This program checks whether 25 is greater than 1 or not. We all know that it is True; that’s why the program is printing the text inside the print().
Let us change the number value to check what happens if the Boolean expression fails? (number < 1).
From the above screenshot, you can observe that it prints nothing because -12 is less than 1, and we don’t have anything to print after the if statement block. Hope you are confused, let us see one more example.
R If Statement example 2
This program for R if statement enables the user to enter any positive integer and check whether the number is Positive integer or Not
# R IF Statement Example number <- as.integer(readline(prompt="Please Enter any integer Value: ")) if (number > 1) { print("You have entered POSITIVE Number") } print("This is not the Message coming from inside the IF STATEMENT") print("This Message is from Outside the IF STATEMENT")
You can observe the output below. It printed all the print statements because 14 is greater than 1.
Let’s try the negative values to fail the condition in R If statement deliberately.
Here, the Boolean expression inside the If statement failed (number > 1). That’s why it prints nothing from the If statement block. However, it printed the statements that are outside the If block.
R If Else statement Syntax
The basic syntax of the If Else in R Programming language is:
if (Boolean_Expression) { #If the Boolean_Expression result is TRUE, these statements will be executed True statements } else { #If the Boolean_Expression result is FALSE, these statements will be executed False statements }
From the above R If else statement code snippet, If the test condition / Boolean expression present in the above syntax is true, then True statements executed. If the condition is false, then False statements executed.
R If Else Statement Flow Chart
The following picture show you the flow chart behind the If Else Statement in R Programming is
- From the R If else flow chart, If the test condition is true, STATEMENT 1 is executed, followed by STATEMENT N.
- If the condition is False, then STATEMENT 2 is executed, followed by STATEMENT N. Here, STATEMENT N executed irrespective of test results. Because it placed outside of the If Else condition block, it has nothing to do with the condition result.
R If Else Statement example
This program allows the user to enter their age, and it checks whether they are eligible to vote or not using the R if else statement.
In this R if else statement program, we are going to place 4 different print statements. If the condition is true, we will print two different statements. If the condition is false, we will print another two statements.
# R IF Else Statement Example my.age <- as.integer(readline(prompt="Please Enter your Age: ")) if (my.age > 18) { print("You are eligible to Vote.") # Statement 1 print("Don't forget to carry Your Voter ID's to Polling booth.") #Statement 2 } else { print("You are NOT eligible to Vote.") #Statement 3 print("We are Sorry") #Statement 4 } print("This Message is from Outside the IF ELSE STATEMENT") #Statement 5
ANALYSIS: The user enters his/her age. If the age is greater than or equal to 18, Statement 1, Statement 2 printed. If the age is less than 18, then Statement 3 and Statement 4 printed as output. Outside the If Else block, we placed one print statement (Statement 5), and irrespective of condition result, this statement executed.
OUTPUT 1: Let us enter the age of 32. Condition is TRUE
OUTPUT 2: Let us enter age = 17 to fail the condition. Condition is FALSE
Nested If Else in R Syntax
The basic syntax of the Nested If Else Statement in R Programming language is as follows:
if (Boolean_Expression 1) { #Boolean_Expression 1 result is TRUE then, it will check for Boolean_Expression 2 if (Boolean_Expression 2) { #Boolean_Expression 2 result is TRUE, then these statements will be executed Boolean_Expression 2 True statements } else { #Boolean_Expression 2 result is FALSE then, these statements will be executed Boolean_Expression 2 False statements } else { #If the Boolean_Expression 1 result is FALSE, these statements will be executed Boolean_Expression 1 False statements }
Nested If Else in R Flow Chart
The following picture shows you the flow chart of the Nested If statement in R Programming.
- If the Test Condition 1 is FALSE, then STATEMENT 3 executed.
- If Test Condition 1 is TRUE, then it will check for the Test Condition 2. And if it is TRUE, then STATEMENT 1 executed. Otherwise, STATEMENT 2 executed.
Nested If Else in R Programming Example
This R Nested If Else program allows the user to enter their age, and then we are going to store it in the variable my.age. If the user-specified age is less than 18, we are going to print two statements. If the condition fails, we check one more condition (Nested If Else), if it succeeds, we print something. And If the nested condition fails, we print some other thing.
# Nested IF Else in R Programming Example my.age <- as.integer(readline(prompt="Please Enter your Age: ")) if (my.age < 18) { print("You are Not a Major.") print("You are Not Eligible to Work") } else { if (my.age >= 18 && my.age <= 60 ) { print("You are Eligible to Work") print("Please fill the Application Form and Email to us") } else { print("As per the Government Rules, You are too Old to Work") print("Please Collect your pension!") } } print("This Message is from Outside the Nested IF Else Statement")
ANALYSIS
Within the R Nested If Else program example, If the specified person’s age is less than 18, then he is not eligible to work. If the age is greater than or equal to 18, the first condition fails, it checks the else statement. Within the Else statement, there is another Boolean expression (called as Nested If Else).
- Nested If Else Statement checks whether the person’s age is greater than or equal to 18 and less than or equal to 60. If the expression is TRUE, then he can apply for the job.
- If the expression result is FALSE, then he is too old to work as per the government.
- We placed one print statement outside the If Else block, and it will execute irrespective of condition result.
OUTPUT 1: We are going to enter age = 35 means the first IF condition is FALSE. It will go to else block, and within the else block, it will check the if (age >= 18 && age <=60), which is TRUE. So, it will print the statements inside this block.
OUTPUT 2: From the below R Nested If Else screenshot, you can observe that we entered the age = 14 means, the first If condition is TRUE. That’s why the statements inside the first if block is executed.
OUTPUT 3: This time, we are going to enter age = 67 means first If condition is FALSE. It will go to else block, and within the else block, it will check the expression if (age >= 18 && age <=60), which is FALSE. That’s why it will print the statements inside Nested else block.
R Else If Statement Syntax
The basic syntax of the Else If Statement in R Programming language is
if (Boolean_Expression 1) { Statement 1 } else if (Boolean_Expression 2) { Statement 2 ......... } else if (Boolean_Expression N) { Statement N } else { Default statements }
The Else If statement in R can handle multiple statements effectively by executing them sequentially. R Else If Statement will check for the first expression, and if the expression is TRUE, then it will execute the statements present in that block. If the expression is FALSE, then it will check the Next one (Else If Boolean expression) and so on. There will be situations where Expression 1, Expression 2 is TRUE, for example:
x = 98, y = 65
Expression 1: if (x != y) //TRUE
Expression 2: else if (x > y) //TRUE
In these situations, statements under the Expression 1 executed because R ELSE IF condition will only be executed if it’s previous IF or ELSE IF statement fails.
Flow Chart of an R Else If Statement
The following screenshot will show you the flow chart behind this Else If in R Programming
R Else If Statement example
This else if in r program allows the user to enter their grand total (total 6 subject marks). Using Else if in R programming, we are going to calculate whether he/she is eligible for a scholarship or not.
# R Else If Statement Example my.marks <- as.integer(readline(prompt="Please Enter your Total Marks: ")) if (my.marks >= 550) { print("Congratulations!!") print("You are eligible for Full Scholarship") } else if (my.marks >= 490) { print("Congratulations!!") print("You are eligible for 50% Scholarship") } else if (my.marks >= 400) { print("Congratulations!!") print("You are eligible for 10% Scholarship") } else { print("You are NOT eligible for Scholarship") print("We are really Sorry for You") }
OUTPUT 1: From the R Else Statement screenshot below, you can observe that We entered my.marks = 562. Here first If condition is TRUE so, statements inside the first if block will be executed.
OUTPUT 2: Here, we enter my.marks = 510 means first If statement in R Else If program is FALSE. It will check the else if (my.marks >= 490), which is TRUE, so it will print the statements within this block. Although else if (my.marks >= 400) condition is TRUE, but it won’t check this condition.
OUTPUT 3: We are going to enter my.marks = 420 means first two statements IF condition, else if (my.marks >= 490) are FALSE. So, It will check the else if (my.marks >= 400), which is TRUE, so it will print statements inside this block.
OUTPUT 4: This time, we enter my.marks = 375 means all the If & Else If conditions Fail. So, It will print the statements within the else block.