Decision Making Statements in R Language:
There are the following variants of if statement in R language.
If Statement
If-else Statement
Multiple if Statement
If else-if ladder
Nested if statement
Switch statement
In R Language we have the following conditional statements:
- Use if to specify a block of code to be executed, if a specified condition is true
- Use else to specify a block of code to be executed, if the same condition is false
- Use else if to specify a new condition to test, if the first condition is false
- Use switch to select one of many blocks of code to be executed
Decision making structures require the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Common infix operators used in if() statements include:
Logical operators are used in If/Else Statement:
&:and
|:or
!:not
Comparison operators are used If/Else Statement::
==:equal
!=:not equal
<: less than
>:greater than
>=:greater than or equal
<=:less than or equal
In [1]:
if(10)
{
print("hello")
}
[1] "hello"
In [2]:
if(0)
{
print("hello")
}
In [3]:
if(-10)
{
print("hello")
}
[1] "hello"
In [8]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
if(a<b)
{
print("A is Smaller")
} else
{
print("B is Smaller")
}
Enter First No: 3467 Enter Second No: 2 [1] "B is Smaller"
In [6]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
if(a<b) {
print("A is smaller")
} else if (a>b) {
print("A is bigger")
} else {
print("Both Are Equal")
}
Enter First No: 3 Enter Second No: 5 [1] "A is smaller"
In [30]:
x = switch(2,"red","green","blue")
print(x)
[1] "green"
In [3]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
print("Press 1 for Addition")
print("Press 2 for Substraction")
print("Press 3 for Multiplication")
print("Press 4 for Division")
y=readline(prompt="Enter your Choice: ")
x = switch(
y,
"1"=cat("Addition=",a+b),
"2"=cat("Subtraction =",a-b),
"3"=cat("Division= ",a/b),
"4"=cat("multiplication =",a*b)
)
Enter First No: 10 Enter Second No: 5 [1] "Press 1 for Addition" [1] "Press 2 for Substraction" [1] "Press 3 for Multiplication" [1] "Press 4 for Division" Enter your Choice: 1 Addition= 15
In [18]:
# Write your code here
a=as.integer(readline(prompt="Enter Number: "))
if(a>0)
{
print("No is +Ve")
} else
{
print("No is -ve")
}
Enter First No: -32 [1] "No is -ve"
In [15]:
# Write your code here
a=as.integer(readline(prompt="Enter Number: "))
if(a%%2==0)
{
print("No is Even")
} else
{
print("No is Odd")
}
Enter First No: 35 [1] "No is Odd"
In [ ]:
# Write your code here
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
if(a<b)
{
print("A is Smaller")
} else
{
print("B is Smaller")
}
In [22]:
# Write your code here
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
c=as.integer(readline(prompt="Enter Third No: "))
if(a<b && a<c)
{
print("A is Smaller")
} else if (b<a && b<c)
{
print("B is Smaller")
} else
{
print("C is Smaller")
}
Enter First No: 4323 Enter Second No: 2 Enter Third No: 4534 [1] "B is Smaller"
In [28]:
# Write your code here
a=as.integer(readline(prompt="Enter Number: "))
if(a>6)
{
if(a%%2==0)
{
print("No is Even")
} else
{
print("No is Odd")
}
} else
{
if(a%%3==0)
{
print("No is divisible by 3")
} else
{
print("No is not divisible by 3 and Less than 6")
}
}
Enter Number: 2 [1] "No is not divisible by 3"
In [ ]: