📖 Scenario: You are analyzing a list of daily temperatures. You want to skip days that are too cold and stop checking once you find a very hot day.
🎯 Goal: Build a program that loops through a list of temperatures, skips cold days below 10 degrees using next, and stops the loop when a temperature reaches or exceeds 30 degrees using break.
📋 What You'll Learn
Create a vector called temperatures with these exact values: 5, 12, 8, 15, 22, 30, 18
Create a variable called cold_threshold and set it to 10
Use a for loop with variable temp to go through temperatures
Inside the loop, use next to skip temperatures less than cold_threshold
Inside the loop, use break to stop if temp is 30 or more
Print each temperature that is processed (not skipped) before the break
💡 Why This Matters
🌍 Real World
Skipping and stopping loops early is useful when processing sensor data or filtering lists based on conditions.
💼 Career
Understanding control flow with next and break helps in data cleaning, automation scripts, and efficient programming.
Progress0 / 4 steps
1
Create the temperature vector
Create a vector called temperatures with these exact values: 5, 12, 8, 15, 22, 30, 18
R Programming
Hint
Use the c() function to create a vector with the given numbers.
2
Set the cold temperature threshold
Create a variable called cold_threshold and set it to 10
R Programming
Hint
Just assign the number 10 to the variable cold_threshold.
3
Loop with next and break statements
Use a for loop with variable temp to go through temperatures. Inside the loop, use next to skip temperatures less than cold_threshold. Use break to stop the loop if temp is 30 or more.
R Programming
Hint
Use for (temp in temperatures) to loop. Use if statements to check conditions and use next and break accordingly.
4
Print the processed temperatures
Run the program to print each temperature that is processed (not skipped) before the loop stops.
R Programming
Hint
The output should show only the temperatures 12, 15, and 22 each on its own line.
Practice
(1/5)
1. What does the next statement do inside a loop in R?
easy
A. Skips the current iteration and moves to the next one
B. Stops the entire loop immediately
C. Restarts the loop from the beginning
D. Exits the current function
Solution
Step 1: Understand the purpose of next
The next statement is used to skip the rest of the current loop iteration and continue with the next iteration.
Step 2: Compare with other statements
Unlike break, which stops the loop completely, next only skips to the next round.
Final Answer:
Skips the current iteration and moves to the next one -> Option A
Quick Check:
next skips iteration [OK]
Hint: Remember: next skips iteration, break stops loop [OK]
Common Mistakes:
Confusing next with break
Thinking next restarts the loop
Believing next exits the function
2. Which of the following is the correct syntax to skip the current iteration in a for loop in R?
easy
A. skip
B. continue
C. break
D. next
Solution
Step 1: Recall R loop control keywords
In R, the keyword to skip the current iteration is next. There is no continue keyword in R.
Step 2: Identify correct keyword
break stops the loop, skip is not a valid keyword.
Final Answer:
next -> Option D
Quick Check:
Use next to skip iteration [OK]
Hint: Use 'next' to skip, not 'continue' in R [OK]
Common Mistakes:
Using 'continue' instead of 'next'
Using 'skip' which is invalid
Confusing 'break' with skipping iteration
3. What will be the output of the following R code?
for(i in 1:5) {
if(i == 3) next
print(i)
}
medium
A. [1] 1 2 4 5
B. [1] 1 2 3 4 5
C. [1] 1 2 3 4
D. [1] 1 2 3 4 5 3
Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. When i == 3, the next statement skips printing 3.
Step 2: Determine printed values
Values 1, 2, 4, and 5 are printed. The number 3 is skipped.
Final Answer:
[1] 1 2 4 5 -> Option A
Quick Check:
next skips 3, so output excludes 3 [OK]
Hint: next skips printing 3, so output misses 3 [OK]
Common Mistakes:
Including 3 in output
Thinking next stops loop
Confusing output format
4. Identify the error in this R code snippet:
for(i in 1:5) {
if(i == 4) next()
print(i)
}
medium
A. The loop variable i is not defined
B. Using parentheses with next causes an error
C. print() cannot be used inside loops
D. The if condition syntax is incorrect
Solution
Step 1: Check usage of next statement
In R, next is a statement, not a function, so it should not have parentheses.
Step 2: Identify syntax error
Using next() causes a syntax error. Correct usage is just next.
Final Answer:
Using parentheses with next causes an error -> Option B
Quick Check:
next is not a function, no parentheses [OK]
Hint: Don't add () after next; it's not a function [OK]
Common Mistakes:
Writing next() instead of next
Assuming next is a function
Misreading error cause
5. Consider the following R code:
sum <- 0
for(i in 1:10) {
if(i %% 2 == 0) next
if(i > 7) break
sum <- sum + i
}
print(sum)
What is the output of this code?
hard
A. 25
B. 20
C. 16
D. 30
Solution
Step 1: Understand loop conditions
The loop runs from 1 to 10. It skips even numbers with next. It stops completely when i > 7 due to break.
Step 2: Calculate sum of odd numbers less than or equal to 7
Odd numbers before break: 1, 3, 5, 7. Sum = 1+3+5+7 = 16.
Final Answer:
16 -> Option C
Quick Check:
Sum odd < 8 = 16 [OK]
Hint: Sum odd numbers until i > 7, skip evens [OK]
Common Mistakes:
Including even numbers in sum
Ignoring break condition
Summing all numbers 1 to 10
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.