0

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
0

3 Answers 3

3

There 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.

0

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.

0

Using cl-loop:

(cl-loop for i from 0 to 5
         unless (< i 2)
         do (message "Value of local variable is %d" i))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.