By next I mean next as found in ruby/perl/python/etc
#!/usr/bin/ruby
for i in 0..5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityThere is no such facility for loops. Considering next
in Ruby is the idiom to avoid a nested if
, you can easily work around this by using this advice the other way around.
(dotimes (i 6)
(unless (< i 2)
(message "Value of local variable is %d" i)))
An even uglier workaround is using the throw
/catch
facility (which is designed for control flow, not exception handling):
(dotimes (i 6)
(catch 'next
(when (< i 2)
(throw 'next t))
(message "Value of local variable is %d" i)))
I'd recommend rethinking the problem so that you don't need next
at all. Or use a more powerful looping construct, such as cl-loop
.
The next statement is just a jump to the end of the loop. Emacs Lisp doesn't have one but it does have "goto":
(dotimes (i 5)
(cl-tagbody
(when (< i 2)
(go next))
(message "Value of i is %d." i)
next))
(Here next
is just an arbitrary label, specifying a target for the
jump.)
I don't actually recommend this style. Prbably rephrasing your loop to
avoid next
is better.
Using cl-loop
:
(cl-loop for i from 0 to 5
unless (< i 2)
do (message "Value of local variable is %d" i))