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

break और continue

break किसी loop को पूरी तरह रुकने को कहता है, जबकि continue इसे इस lap का बाकी हिस्सा skip करके सीधे अगले पर जाने को कहता है।
Syntax
go
for condition {
    if cond1 {
        break
    }
    if cond2 {
        continue
    }
}

Using break

break तुरंत सबसे नज़दीकी enclosing for loop रोक देता है, बाकी बची किसी भी iteration को skip करते हुए, और execution loop के ठीक बाद वाले code से जारी रहता है।

उदाहरण: Using break

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Loop: repeat while the condition holds
	for i := 0; i < 10; i++ {
		// Check whether i == 4
		if i == 4 {
			break
		}
		// Print the values followed by a newline
		fmt.Println(i)
	}
}

Using continue

continue current iteration के body का बाकी हिस्सा skip करता है और सीधे loop की अगली iteration पर jump करता है (three-part for loop में पहले post statement चलाते हुए)।

उदाहरण: Using continue

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Loop: repeat while the condition holds
	for i := 0; i < 6; i++ {
		// Check whether i%2 == 0
		if i%2 == 0 {
			// Skip the rest of this iteration
			continue
		}
		// Print the values followed by a newline
		fmt.Println("odd:", i)
	}
}

break Inside switch

चूंकि switch भी break accept करता है, किसी loop के अंदर बैठे switch के अंदर लिखा एक break statement सिर्फ switch से बाहर निकलता है -- यह loop से भी बाहर नहीं निकलता। यह उन languages के आदी लोगों को हैरान करता है जहां break हमेशा सबसे नज़दीकी loop को target करता है।

Note: याद रखें: switch में एक bare break switch को रोकता है, उस loop को नहीं जिसके अंदर यह है।

उदाहरण: break Inside switch

markup
package main

import "fmt"

func main() {
	for i := 0; i < 3; i++ {
		switch i {
		case 1:
			break // only exits the switch, loop continues
		default:
			fmt.Println("processing", i)
		}
	}
}

Labeled break and continue

किसी nested loop के अंदर से एक outer loop को break या continue करने के लिए, Go आपको outer loop को label करने और उस label को reference करने देता है, goto या flag variables का सहारा लिए बिना सटीक control देते हुए।

उदाहरण: Labeled break and continue

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
Outer:
	for i := 0; i < 3; i++ {
		// Loop: repeat while the condition holds
		for j := 0; j < 3; j++ {
			// Check whether j == 1
			if j == 1 {
				continue Outer
			}
			// Print the values followed by a newline
			fmt.Println(i, j)
		}
	}
}
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. किसी loop में nested switch के अंदर break उपयोग करना, इसके loop से बाहर निकलने की उम्मीद करते हुए, जबकि यह असल में सिर्फ switch से बाहर निकलता है।
  2. यह भूल जाना कि labeled break/continue मौजूद हैं, और इसके बजाय nested loops से बचने के लिए goto या एक boolean flag variable का सहारा लेना।
  3. continue को ऐसे code के बाद रखना जो हर iteration में हमेशा चलना चाहिए, गलती से हर बार execute होने वाले cleanup logic को skip करते हुए।
चैप्टर सारांश
  • break तुरंत सबसे नज़दीकी enclosing for loop (या switch/select) से बाहर निकलता है।
  • continue current iteration का बाकी हिस्सा skip करता है और loop की अगली iteration पर जाता है।
  • एक labeled break या continue किसी nested loop के अंदर से एक outer loop को target कर सकता है।
  • switch के अंदर break switch से बाहर निकलता है, किसी surrounding loop से नहीं -- अगर आपको loop से बाहर निकलना है तो एक label उपयोग करें।
🔒

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.