Welcome to the Simplilearn Community

Want to join the rest of our members? Sign up right away!

Sign Up

Data Science with R | Arindam Dev | June 05 - July 04 | 2021

I have a question :

for example, if i type the following code :
x <- 100
if (x > 1) {
answer <-"Greater than 1"
} else{
answer<-"Less than 1"
}

This code works fine

however, if I place else in the next line, it doesn't work. Can you please help me understand this behaviour?

also, why can't we put if & else statements within the same { }
 
I have a question :

for example, if i type the following code :
x <- 100
if (x > 1) {
answer <-"Greater than 1"
} else{
answer<-"Less than 1"
}

This code works fine

however, if I place else in the next line, it doesn't work. Can you please help me understand this behaviour?

also, why can't we put if & else statements within the same { }
As per my knowledge for your 1st question i think it is standard syntax to use if and else in R and i didn't get your second question
 
Top