break और continue
for condition {
if cond1 {
break
}
if cond2 {
continue
}
}
Using break
break तुरंत सबसे नज़दीकी enclosing for loop रोक देता है, बाकी बची किसी भी iteration को skip करते हुए, और execution loop के ठीक बाद वाले code से जारी रहता है।
उदाहरण: Using break
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
Using continue
continue current iteration के body का बाकी हिस्सा skip करता है और सीधे loop की अगली iteration पर jump करता है (three-part for loop में पहले post statement चलाते हुए)।
उदाहरण: Using continue
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
break Inside switch
चूंकि switch भी break accept करता है, किसी loop के अंदर बैठे switch के अंदर लिखा एक break statement सिर्फ switch से बाहर निकलता है -- यह loop से भी बाहर नहीं निकलता। यह उन languages के आदी लोगों को हैरान करता है जहां break हमेशा सबसे नज़दीकी loop को target करता है।
उदाहरण: break Inside switch
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)
}
}
}
Login to try C/C++/Java/PHP code in the editor
Labeled break and continue
किसी nested loop के अंदर से एक outer loop को break या continue करने के लिए, Go आपको outer loop को label करने और उस label को reference करने देता है, goto या flag variables का सहारा लिए बिना सटीक control देते हुए।
उदाहरण: Labeled break and continue
// 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)
}
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी loop में nested switch के अंदर break उपयोग करना, इसके loop से बाहर निकलने की उम्मीद करते हुए, जबकि यह असल में सिर्फ switch से बाहर निकलता है।
- यह भूल जाना कि labeled break/continue मौजूद हैं, और इसके बजाय nested loops से बचने के लिए goto या एक boolean flag variable का सहारा लेना।
- 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: