...

Artificial Intelligence - R Programming Basic

Back to Course

Lesson Description


Lession - #808 R Statements


Statements in R

Statements in programming are used for decision making purpose.The if statement is the simplest decision-making statement which helps us to take a decision on the basis of the condition.if else statement in R evaluates whether an expression is true or false & execute the code or print statement inside body.

SNO STATEMENT DESCRIPTION EXAMPLE
1 IF This statement can be used to test a condition & the result can be used to make decision with the keyword IF. x <-10
y<-15
if(y>x>

{
cat(x,"is a smaller number than\n",y>

}

Output:

10 is a smaller number than 15
2 IF ELSE This statement can be used when previous statement was not TRUE. x1 = 11
if(x1 %% 2 == 0>

{
cat(x1,"is an Even number">

} else {
cat(x1,"is an Odd number">

}

Output:

11 is an Odd number
3 ELSE if This statement can be used when previous statement was not TRUE. x <- 100
y <- 100
if (y > x>
{
print("y is greater than x">

} else if (x == y>
{
print ("x and y are equal">

}

Output:

[1] "x and y are equal"



Try it here