← Back to Ruby Course | Chapter 6: Control Flow | Lesson 7 of 7

break, next और redo

break पूरी तरह एक loop छोड़ता है, next अगली turn तक skip करता है, और redo current turn repeat करता है। यह एक game छोड़ने, अपनी turn skip करने, या अपनी turn फिर try करने जैसा है।

In this page:

  1. break, next, और redo
Syntax
ruby
loop do
  next if skip_condition
  break if stop_condition
  # body
end

break, next, और redo

break nearest enclosing loop से पूरी तरह exit करता है, next अगले iteration तक skip करता है (दूसरी languages में continue जैसा), और redo loop की condition फिर से check किए बिना current iteration को शुरू से फिर चलाता है।

redo rarely इस्तेमाल होता है लेकिन एक single iteration के अंदर retry-style logic के लिए handy हो सकता है। तीनों while, until, for, और each जैसे block-based iteration के अंदर काम करते हैं।

Note: redo Ruby का सबसे कम common loop control keyword है -- इसके लिए सिर्फ तब reach करें जब आपको specifically current iteration repeat करनी हो, अगले पर move नहीं करना हो।

उदाहरण: break, next, and redo

markup
(1..10).each do |i|
  next if i.even?
  break if i > 7
  puts i
end
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि break सारे nested loops छोड़ देगा, जब यह सिर्फ innermost से exit करता है।
  2. next if को skip होने वाले code के बाद रखना, इसलिए वे lines फिर भी चलती हैं।
  3. कुछ बदले बिना redo इस्तेमाल करना, जो same iteration हमेशा के लिए repeat करता है।
चैप्टर सारांश
  • if, elsif, else, unless, और case/when paths के बीच choose करते हैं।
  • while, until, for, each, और loop do code repeat करते हैं।
  • break, next, और redo loop flow control करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.