0
0
R Programmingprogramming~10 mins

If-else statements in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Positive" if x is greater than 0.

R Programming
x <- 5
if (x [1] 0) {
  print("Positive")
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will check if x is less than 0, which is wrong here.
2fill in blank
medium

Complete the code to print "Even" if number is divisible by 2.

R Programming
number <- 4
if (number %% 2 [1] 0) {
  print("Even")
}
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will check if remainder is not zero, which is incorrect for even numbers.
3fill in blank
hard

Fix the error in the if-else statement to print "Positive" or "Non-positive".

R Programming
x <- -3
if (x [1] 0) {
  print("Positive")
} else {
  print("Non-positive")
}
Drag options to blanks, or click blank then click option'
A==
B>
C<-
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' or '<-' inside the if condition causes syntax errors.
4fill in blank
hard

Fill in the blank to print "Adult" if age is 18 or more, otherwise "Minor".

R Programming
age <- 20
if (age [1] 18) {
  print("Adult")
} else {
  print("Minor")
}
Drag options to blanks, or click blank then click option'
A>
B<
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in the if condition would reverse the logic.
5fill in blank
hard

Fill in the blanks to print "Positive", "Zero", or "Negative" based on x value.

R Programming
x <- 0
if (x [1] 0) {
  print("Positive")
} else if (x [2] 0) {
  print("Zero")
} else {
  print("Negative")
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '>' in the first condition can cause wrong output.