break, next और redo
break पूरी तरह एक loop छोड़ता है, next अगली turn तक skip करता है, और redo current turn repeat करता है। यह एक game छोड़ने, अपनी turn skip करने, या अपनी turn फिर try करने जैसा है।
In this page:
Syntax
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
(1..10).each do |i|
next if i.even?
break if i > 7
puts i
end
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- यह उम्मीद करना कि
breakसारे nested loops छोड़ देगा, जब यह सिर्फ innermost से exit करता है। next ifको skip होने वाले code के बाद रखना, इसलिए वे lines फिर भी चलती हैं।- कुछ बदले बिना
redoइस्तेमाल करना, जो same iteration हमेशा के लिए repeat करता है।
चैप्टर सारांश
if,elsif,else,unless, औरcase/whenpaths के बीच choose करते हैं।while,until,for,each, औरloop docode repeat करते हैं।break,next, औरredoloop flow control करते हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: