The if-statement in Programming Language alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here comes the R Programming Language else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.
Syntax of if-else statement in R Language
if (condition) {
# code to be executed if condition is TRUE
} else {
# code to be executed if condition is FALSE
}
if-else statement in RWorking of if-else statements in R Programming
- Control falls into the if block.
- The flow jumps to Condition.
- Condition is tested.
- If the Condition yields true, goto Step 4.
- If the Condition yields false, goto Step 5.
- The if-block or the body inside the if is executed.
- The else block or the body inside the else is executed.
- Flow exits the if-else block.
Flowchart if-else statement in R
R - if-else statementConditions and If Statements
Here we will show the use of logical conditions in if statements. Adjust the values and conditions as needed for our specific requirements.
|
| Equal | == | x == y |
| Not equal | != | x != y |
| Greater than | > | a > b |
| Less than | < | x < y |
| Greater than or equal to | >= | x >= y |
| Less than or equal to | <= | x <= y |
Ifelse Function in R Programming
R
x <- 5
# Check value is less than or greater than 10
if(x > 10)
{
print(paste(x, "is greater than 10"))
} else
{
print(paste(x, "is less than 10"))
}
Output
[1] "5 is less than 10"
Here in the above code, Firstly, x is initialized to 5, then the if-condition is checked(x > 10), and it yields false. Flow enters the else block and prints the statement "5 is less than 10".
Ifelse Function in R Programming
R
x <- 5
# Check if value is equal to 10
if(x == 10)
{
print(paste(x, "is equal to 10"))
} else
{
print(paste(x, "is not equal to 10"))
}
Output
[1] "5 is not equal to 10"
Nested if-else statement in R
The if-else statements in R can be nested together to form a group of statements and evaluate expressions based on the conditions one by one, beginning from the outer condition to the inner one by one respectively. An if-else statement within another if-else statement in R better justifies the definition.
Syntax
if(condition1){
# execute only if condition 1 satisfies
if(condition 2){
# execute if both condition 1 and 2 satisfy
}
}else{
}Ifelse Function in R Programming
R
# define a variable
x <- 15
# check the value of x using nested if-else statements
if (x < 10) {
# if x is less than 10
print("x is less than 10")
} else {
# if x is greater than or equal to 10
if (x < 20) {
# if x is less than 20
print("x is between 10 and 20")
} else {
# if x is greater than or equal to 20
print("x is greater than or equal to 20")
}
}
Output
[1] "x is between 10 and 20"
- In this example, we first define a variable x with the value of 15.
- Then we use a nested if-else statement to check the value of x.
- The outer if-else statement checks if x is less than 10. If it is, we print the message "x is less than 10". If x is not less than 10, we move on to the nested if-else statement inside the else block.
- This nested if-else statement checks if x is less than 20. If it is, we print the message "x is between 10 and 20". If x is not less than 20, we print the message "x is greater than or equal to 20". Since x is 15, the code will print "x is between 10 and 20".
Ifelse Function in R Programming
R
# define variables for grades and income
grades <- 85
income <- 25000
# check eligibility for scholarship using nested if-else statements
if (grades >= 80) {
# if grades are 80 or above
if (income <= 30000) {
# if income is 30,000 or less
print("Congratulations, you are eligible for a scholarship!")
} else {
# if income is more than 30,000
print("Sorry, your income is too high to qualify for a scholarship.")
}
} else {
# if grades are below 80
print("Sorry, your grades are too low to qualify for a scholarship.")
}
Output
[1] "Congratulations, you are eligible for a scholarship!"
- In this example, we first define two variables, grades and income, to represent a student's grades and income, respectively.
- Then we use a nested if-else statement to check if the student is eligible for a scholarship.
- The outer if-else statement checks if the student's grades are 80 or above. If they are, we move on to the nested if-else statement inside the if block. This nested if-else statement checks if the student's income is 30,000 or less.
- If it is, we print the message "Congratulations, you are eligible for a scholarship!" If the student's income is more than 30,000, we print the message "Sorry, your income is too high to qualify for a scholarship." If the student's grades are below 80, we print the message "Sorry, your grades are too low to qualify for a scholarship."
- This nested if-else statement can be expanded upon to include additional conditions, such as residency or enrollment status, to determine scholarship eligibility.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o
5 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
9 min read