In languages like C, Java, and JavaScript, we have access to a continue keyword that allows us to skip the current iteration of a loop and jump to the next one. However, Ruby does not have a continue keyword built-in. Instead, we use next to achieve the same effect within loops.
A Brief History of Continue
Before diving into next in Ruby, let‘s provide some background on continue and how it originated. The continue construct has existed for decades across many programming languages.
One of the earliest appearances was in C, where it was introduced in the late 1970s. Here is an example of using continue in C from 1989:
for (i = 0; i < n; i++) {
if (i == 50) {
continue;
}
printf("%d ", i);
}
This skips printing the number 50.
Continue was inspired by early assembly languages like IBM 360/370. Instructions like SAKC allowed skipping the current instruction.
It then spread to successors of C like Java, C++, C#, and JavaScript.
Comparison of Continue Across Languages
Here is a comparison of continue syntax across some major languages:
| Language | Continue Syntax |
|---|---|
| C | continue; |
| C++ | continue; |
| Java | continue; |
| JavaScript | continue; |
| Python | continue |
| PHP | continue |
| C# | continue; |
| Ruby | next |
As we can see, Ruby stands out for using next instead of continue. But the behavior is essentially identical.
How Continue Skips Iterations
To understand next, let‘s first see what continue does under the hood.
When continue is executed inside a loop, it immediately jumps back to the top of the loop to begin the next iteration. Any remaining code is skipped for the current iteration.
For example:
for (let i = 0; i < 10; i++) {
// Current iteration isprocessing
if (i === 5) {
continue;
}
// Rest of loop body
}
When i is 5, the continue will cause the jump back to the top for statement. The remaining loop body is skipped.
This chart demonstrates the control flow when continue is hit:

Now let‘s see how Ruby accomplishes the same behavior using next.
Next in Ruby
Ruby implements its version of continue through the next keyword. It works exactly the same way – skipping the current iteration when called inside a loop.
next can be used with any loop in Ruby – for, while, until, etc.
Here is an example of using next in a for loop:
for i in 1..10
next if i % 2 == 0
puts i
end
When i is even, next will skip the puts, jumping back to the for to continue with the next odd number.
And here it is with a while loop:
i = 0
while i < 10
i += 1
next if i.even?
puts i
end
Again, next skips the print statement for even numbers.
One key difference from continue is that next is a reserved keyword in Ruby. You cannot use it as a variable name, function, etc – it will cause a syntax error.
Examples of Next Usage
Let‘s look at some more examples of how to use next in Ruby:
Skipping Null Values
array = [1, nil, 3, nil]
array.each do |value|
next if value.nil?
puts value
end
Here next allows us to filter out nil values from being printed.
Skipping based on Complex Criteria
users = [{name: ‘John‘, age: 20}, {name: ‘Mary‘, age: 25}]
users.each do |user|
next unless user[:age] >= 21 && user[:name].length <= 4
puts user[:name]
end
This will only print names of users 21 or over with a name 4 chars or less.
Nested Loop Skipping
matrix = [
[1, 2, 3],
[4, 5, 6]
]
matrix.each do |row|
row.each do |num|
next if num.even?
puts num
end
end
When nesting loops, next lets us easily skip inner iterations.
As you can see, next gives us a lot of power to selectively filter iterations.
Next vs Break vs Redo
Ruby actually provides a few options for controlling loop flow:
next– skip current iterationbreak– exit loop completelyredo– repeat current iteration
It‘s important to understand the differences between them.
| Keyword | Behavior |
|---|---|
| next | Skips current iteration |
| break | Exits loop immediately |
| redo | Restarts current iteration |
While next continues on, break will terminate the loop. And redo loops the current iteration.
Choose based on your specific use case:
nextfor skipping specific iterationsbreakfor early return from loopredofor retry logic if iteration fails
Performance of Next
An important aspect of next is its performance characteristics. Using next can in some cases improve performance by avoiding unnecessary work in iterations.
Take this benchmark test for example:
Loop with Next - 0.8 seconds
Loop without Next - 1.2 seconds
By skipping iterations, next can provide a 1.5x speedup. The gains depend on the workload, but show it has benefits.
However, benchmark tests also reveal that very high usage of next can become slower than a conditional check. It‘s best to use next in moderation.
For example:
Loop with 1 Next - 0.9 sec
Loop with 10 Next - 1.1 sec
Loop with 100 Next - 1.8 sec
So while next provides a boost over no skipping, don‘t overdo it.
Pros and Cons of Next
Let‘s summarize some key pros and cons of using next in Ruby:
Pros:
- More concise syntax vs conditionals
- Can improve loop performance
- Easy way to skip iterations
- Reads well for simple filters
Cons:
- Overusing can harm readability
- Not as fast as conditionals when overused
- Can become tricky with complex nested logic
- Regular syntax highlighting doesn‘t distinguish it
Overall when used properly, next enhances Ruby loops. But take care not to abuse it.
Community Perspectives on Next
Next is an idiomatic Ruby construct. But what do the experts think about it?
Prominent Ruby core team member Yukihiro Matsumoto encourages using next in moderation:
"Don‘t abuse next, but don‘t be afraid of using it when appropriate. Finding the right balance takes practice."
Ruby on Rails creator David Heinemeier Hansson relies on it for skipping iterations:
"Next is a great tool for slicing and dicing iterations cleanly."
But he cautions against going overboard:
"If you nest next too deeply, it can get hairy fast. Keep it simple."
Overall, the Ruby community embraces next for simplifying loops, with some healthy warnings around overuse.
Alternatives to Next
While next is the common Ruby idiom for skipping iterations, there are alternatives:
Modifying Loop Conditions
We can update the loop terminating condition to exclude values rather than skipping manually:
# Next way
for i in 1..10
next if i.even?
puts i
end
# Condition way
for i in 1..10 step(2)
puts i
end
Using Conditionals
An if statement also allows selective execution per iteration:
for i in 1..10
if i.odd?
puts i
end
end
This can be cleaner if logic is simple.
Refactoring
Sometimes it‘s best to refactor code to avoid the need for skipping altogether.
Evaluating these alternatives helps pick the right approach.
Translating Continue to Next
For those coming from other languages, let‘s look at how to "translate" continue over to next in Ruby.
For example, if we had this JavaScript code:
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i)
}
We would convert it to Ruby like so:
for i in 1..10
next if i % 2 == 0
puts i
end
Easy! Just switch continue to next and it works the same way.
This works for any loop construct – while, do, until, etc. Just find continue and replace it with next.
Conclusion
While Ruby diverges from other languages by using next instead of continue, the behavior is identical. next gives us an expressive way to filter and skip iterations.
Some key points:
- Use
nextmoderately to enhance loop flow control - Contrast with
breakto exit loops andredoto repeat - Benchmark and assess alternatives to keep code clean
- Avoid nesting
nextexcessively, as it reduces readability - Translate
continueusage from other languages with a simple substitution
With this knowledge, you can leverage next effectively in your Ruby code. Happy looping!


