Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

R Nested If


Sure! Nested "if" statements in R allow you to have multiple levels of conditions within your code. Each "if" statement can have its own "else" block, and you can place another "if" statement inside either the "if" or "else" block of another "if" statement.

Here's a simple explanation with an example:

Let's say you want to categorize a person's age into different groups: "child", "teenager", "adult", and "senior". You can use nested "if" statements to achieve this.

Example

# Assign the age to a variable
age <- 25

# Check if the age falls into different categories
if (age < 13) {
print("You are a child")
} else {
if (age < 20) {
print("You are a teenager")
} else {
if (age < 65) {
print("You are an adult")
} else {
print("You are a senior")
}
}
}

Explanation:

So, based on the value of the age variable, the appropriate message will be printed.


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java