📖 Scenario: Imagine you are organizing a small event and need to decide what to do based on the weather forecast.
🎯 Goal: You will write a simple R program that uses control flow to decide what activity to do depending on the weather.
📋 What You'll Learn
Create a variable called weather with the value "sunny"
Create a variable called activity and set it to an empty string
Use an if statement to set activity to "Go for a walk" if weather is "sunny"
Use an else if statement to set activity to "Read a book indoors" if weather is "rainy"
Use an else statement to set activity to "Stay home and relax" for any other weather
Print the value of activity
💡 Why This Matters
🌍 Real World
Control flow is used in real life to make decisions, like choosing what to do based on the weather or other conditions.
💼 Career
Understanding control flow is essential for programming jobs because it allows software to react differently depending on inputs or situations.
Progress0 / 4 steps
1
Create the weather variable
Create a variable called weather and set it to the string "sunny".
R Programming
Hint
Use the assignment operator <- to assign the value "sunny" to weather.
2
Create the activity variable
Create a variable called activity and set it to an empty string "".
R Programming
Hint
Assign an empty string "" to the variable activity.
3
Use if-else to decide the activity
Use an if statement with the condition weather == "sunny" to set activity to "Go for a walk". Then use else if with weather == "rainy" to set activity to "Read a book indoors". Finally, use else to set activity to "Stay home and relax".
R Programming
Hint
Use if, else if, and else blocks to check the value of weather and assign the correct string to activity.
4
Print the activity
Write a print() statement to display the value of the variable activity.
R Programming
Hint
Use print(activity) to show the chosen activity.
Practice
(1/5)
1. What does control flow in R programming mainly do?
easy
A. Creates new variables without declaration
B. Changes the data types of variables automatically
C. Decides which parts of the code run and when
D. Prints output to the console
Solution
Step 1: Understand the role of control flow
Control flow guides the program to run certain parts of code based on conditions or loops.
Step 2: Compare options with control flow purpose
Only Decides which parts of the code run and when matches this role; others describe unrelated actions.
Final Answer:
Decides which parts of the code run and when -> Option C
Quick Check:
Control flow = directs execution [OK]
Hint: Control flow controls code execution order [OK]
Common Mistakes:
Confusing control flow with variable creation
Thinking control flow changes data types
Assuming control flow prints output
2. Which of the following is the correct syntax for an if statement in R?
easy
A. if x > 5 then print("Yes")
B. if (x > 5) { print("Yes") }
C. if x > 5: print("Yes")
D. if (x > 5) print "Yes"
Solution
Step 1: Recall R if statement syntax
In R, the if statement requires parentheses around the condition and braces for the code block.
Step 2: Check each option
if (x > 5) { print("Yes") } uses correct parentheses and braces. Options B, C, and D use incorrect syntax styles.
Final Answer:
if (x > 5) { print("Yes") } -> Option B
Quick Check:
R if syntax = if(condition) { code } [OK]
Hint: Use parentheses and braces for if in R [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword (not in R)
Missing braces for code block
3. What will be the output of this R code?
for (i in 1:3) {
if (i == 2) {
print("Skip")
next
}
print(i)
}
medium
A. [1] 1
[1] "Skip"
[1] 3
B. [1] 1
[1] 2
[1] 3
C. [1] 1
[1] 3
D. [1] "Skip"
[1] 3
Solution
Step 1: Trace the for loop iterations
The loop runs i = 1, 2, 3. For i=1, it prints 1. For i=2, it prints "Skip" and uses next to skip printing i. For i=3, it prints 3.
Step 2: Understand the effect of next
next skips the rest of the loop body for i=2, so print(i) is not called then.
Final Answer:
[1] 1
[1] "Skip"
[1] 3 -> Option A
Quick Check:
next skips print(i) at i=2 [OK]
Hint: next skips current loop iteration [OK]
Common Mistakes:
Expecting number 2 to print
Ignoring next statement effect
Confusing print order
4. Identify the error in this R code snippet:
i <- 1
while (i < 4) {
print(i)
i = i + 1
}
medium
A. The while loop condition is incorrect
B. The increment uses '=' instead of '<-'
C. Missing braces around the while loop body
D. No error, code runs correctly
Solution
Step 1: Check while loop syntax
The while loop uses parentheses for condition and braces for the body, which is correct.
Step 2: Check variable increment
In R, '=' can be used for assignment inside code blocks, so 'i = i + 1' works fine.
Final Answer:
No error, code runs correctly -> Option D
Quick Check:
Both '=' and '<-' assign values in R [OK]
Hint: Both '=' and '<-' assign in R [OK]
Common Mistakes:
Thinking '=' is invalid for assignment
Expecting syntax error without braces
Confusing loop condition syntax
5. You want to print numbers from 1 to 5 but skip 3 using control flow in R. Which code correctly does this?
hard
A. for (i in 1:5) { if (i == 3) next; print(i) }
B. for (i in 1:5) { if (i == 3) break; print(i) }
C. for (i in 1:5) { print(i); if (i == 3) next }
D. for (i in 1:5) { if (i == 3) print(i); else next }
Solution
Step 1: Understand the goal
We want to print numbers 1, 2, 4, 5 but skip printing 3.
Step 2: Analyze each option
for (i in 1:5) { if (i == 3) next; print(i) } uses 'next' to skip printing when i is 3, printing all others. for (i in 1:5) { if (i == 3) break; print(i) } stops loop at 3 (break), so 4 and 5 won't print. for (i in 1:5) { print(i); if (i == 3) next } prints i first then next skips the rest of the body (which is empty), so prints all including 3. for (i in 1:5) { if (i == 3) print(i); else next } prints only 3 and skips others, which is wrong.
Step 3: Choose the best control flow usage
Only for (i in 1:5) { if (i == 3) next; print(i) } correctly uses next to skip exactly 3 and continue the loop.
Final Answer:
for (i in 1:5) { if (i == 3) next; print(i) } -> Option A
Quick Check:
next skips iteration cleanly [OK]
Hint: Use next to skip unwanted loop iterations [OK]
Common Mistakes:
Using break stops loop early
Printing only the skipped number
Not using next to skip iteration
Placing print before the next check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.