0
0
R Programmingprogramming~20 mins

Why control flow directs execution in R Programming - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Control Flow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this R code with if-else?
Consider the following R code snippet. What will it print?
R Programming
x <- 5
if (x > 3) {
  print("Greater")
} else {
  print("Smaller")
}
A[1] "Smaller"
BError: object 'x' not found
C[1] "Greater"
D[1] "Equal"
Attempts:
2 left
💡 Hint
Check the condition x > 3 and what it evaluates to.
Predict Output
intermediate
2:00remaining
What does this for loop print in R?
Look at this R code. What will be the output?
R Programming
for (i in 1:3) {
  if (i == 2) next
  print(i)
}
A[1] 2
B
[1] 1
[1] 2
[1] 3
CError: unexpected symbol in "next print"
D
[1] 1
[1] 3
Attempts:
2 left
💡 Hint
The 'next' statement skips the current iteration.
Predict Output
advanced
2:00remaining
What is the output of this nested if-else in R?
Analyze this R code and select the correct output.
R Programming
x <- 0
if (x > 0) {
  print("Positive")
} else if (x == 0) {
  print("Zero")
} else {
  print("Negative")
}
A[1] "Positive"
B[1] "Zero"
C[1] "Negative"
DError: unexpected 'else' in "} else"
Attempts:
2 left
💡 Hint
Check the value of x and which condition matches.
Predict Output
advanced
2:00remaining
What happens when this R code runs with break inside a while loop?
What will this R code print?
R Programming
i <- 1
while (i <= 5) {
  if (i == 3) break
  print(i)
  i <- i + 1
}
A
[1] 1
[1] 2
B
[1] 1
[1] 2
[1] 3
C[1] 3
DInfinite loop
Attempts:
2 left
💡 Hint
The break statement stops the loop when i equals 3.
Predict Output
expert
3:00remaining
What is the value of x after this R code with nested control flow?
Consider this R code. What is the final value of x?
R Programming
x <- 0
for (i in 1:4) {
  if (i %% 2 == 0) {
    x <- x + i
  } else {
    x <- x - i
  }
}
A2
B-2
C0
D4
Attempts:
2 left
💡 Hint
Add or subtract i depending on whether it is even or odd.