Decision-Making Statements in R
Decision-Making Statements in R
Introduction
Decision-making statements are control structures that enable a program to choose different courses of action based on specified conditions. They allow the program to make decisions and execute different blocks of code depending on whether a condition evaluates to TRUE or FALSE.
In R, decision-making statements are used to:
- Test conditions.
- Execute specific statements when conditions are satisfied.
- Select one block of code among several alternatives.
- Implement logical decisions in programs.
The major decision-making constructs in R are:
- if Statement
- if-else Statement
- if-else-if Ladder
- Nested if Statement
- switch() Statement
- ifelse() Function
1. if Statement
Definition
The if statement executes a block of code only when a specified condition is TRUE.
Syntax
if(condition)
{
statements
}
Flowchart
Condition
|
+----+----+
| |
TRUE FALSE
| |
Execute Skip
statements
Example
marks <- 80
if(marks >= 50)
{
print("Pass")
}
Output
[1] "Pass"
Applications
- Positive/negative number check
- Even/odd number check
- Eligibility verification
2. if-else Statement
Definition
The if-else statement provides two alternative execution paths. One block is executed when the condition is TRUE, and another block is executed when the condition is FALSE.
Syntax
if(condition)
{
statements1
}
else
{
statements2
}
Flowchart
Condition
|
+-----+-----+
| |
TRUE FALSE
| |
Execute if Execute else
block block
Example
num <- 7
if(num %% 2 == 0)
{
print("Even Number")
}
else
{
print("Odd Number")
}
Output
[1] "Odd Number"
Applications
- Pass/Fail determination
- Even/Odd checking
- Voting eligibility
3. if-else-if Ladder
Definition
The if-else-if ladder is used when multiple conditions need to be tested. The conditions are checked sequentially until one of them becomes TRUE.
Syntax
if(condition1)
{
statements1
}
else if(condition2)
{
statements2
}
else if(condition3)
{
statements3
}
else
{
statements4
}
Example
marks <- 82
if(marks >= 90)
{
grade <- "A"
}
else if(marks >= 75)
{
grade <- "B"
}
else if(marks >= 60)
{
grade <- "C"
}
else
{
grade <- "F"
}
print(grade)
Output
[1] "B"
Applications
- Grade calculation
- Salary classification
- Menu selection
4. Nested if Statement
Definition
An if statement inside another if statement is called a nested if statement.
Syntax
if(condition1)
{
if(condition2)
{
statements
}
}
Example
age <- 20
citizen <- TRUE
if(age >= 18)
{
if(citizen)
{
print("Eligible to vote")
}
}
Output
[1] "Eligible to vote"
Applications
- Multi-level decision making
- Admission eligibility
- Authentication systems
5. switch() Statement
Definition
The switch() function selects one option from multiple alternatives. It behaves similarly to the switch-case statement found in other programming languages.
Syntax
switch(expression,
case1,
case2,
...)
Example 1: Numeric Expression
choice <- 2
fruit <- switch(choice,
"Apple",
"Orange",
"Mango")
print(fruit)
Output
[1] "Orange"
Example 2: Character Expression
operation <- "add"
result <- switch(operation,
add = 10 + 20,
sub = 10 - 20,
mul = 10 * 20)
print(result)
Output
[1] 30
Applications
- Menu-driven programs
- Calculator programs
- Multiple-choice selection
6. ifelse() Function
Definition
The ifelse() function performs vectorized conditional operations. It returns one value when the condition is TRUE and another when it is FALSE.
Syntax
ifelse(condition,
value_if_true,
value_if_false)
Example
marks <- c(80, 40, 90, 30)
result <- ifelse(marks >= 50,
"Pass",
"Fail")
print(result)
Output
[1] "Pass" "Fail" "Pass" "Fail"
Applications
- Assigning grades
- Creating new columns in data frames
- Vectorized computations
Comparison of Decision-Making Statements
| Statement | Number of Conditions | Number of Alternatives |
|---|---|---|
| if | One | One |
| if-else | One | Two |
| if-else-if | Multiple | Multiple |
| Nested if | Multiple (hierarchical) | Multiple |
| switch() | Multiple choices | One selected choice |
| ifelse() | Vectorized condition | Two |
Example Program
marks <- 78
if(marks >= 90)
{
grade <- "A"
}
else if(marks >= 75)
{
grade <- "B"
}
else if(marks >= 60)
{
grade <- "C"
}
else
{
grade <- "F"
}
print(paste("Grade =", grade))
Output
[1] "Grade = B"
Summary
| Construct | Purpose |
|---|---|
if | Execute statements when condition is TRUE |
if-else | Select between two alternatives |
if-else-if | Select among multiple alternatives |
Nested if | Hierarchical decision making |
switch() | Choose one option from many |
ifelse() | Vectorized conditional evaluation |
Conclusion
Decision-making statements are fundamental control structures in R that allow programs to respond differently to different situations. The if, if-else, and if-else-if constructs provide conditional execution, while nested if statements handle complex decisions. The switch() statement is useful for menu-driven applications, and the ifelse() function supports efficient vectorized conditional operations. These constructs are essential for developing logical, interactive, and intelligent programs in R and form the basis for more advanced programming techniques.
Comments
Post a Comment