for Loop क्या है
for init; condition; post {
// body
}
for condition {
// body
}
for {
// infinite loop
}
The Classic Three-Part for Loop
Go का सबसे परिचित for loop semicolons से अलग तीन हिस्सों वाला है: एक initializer, हर iteration से पहले जांची जाने वाली एक condition, और हर iteration के बाद चलने वाला एक post statement। यह कई languages में मिलने वाले classic C-style for loop जैसा है।
उदाहरण: The Classic Three-Part for Loop
// 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 < 5; i++ {
// Print the values followed by a newline
fmt.Println("iteration", i)
}
}
Login to try C/C++/Java/PHP code in the editor
for as a while Loop
init और post clauses हटाकर सिर्फ एक condition रखना for को दूसरी languages में मिलने वाले while loop के बराबर बना देता है -- यह तब तक चलता रहता है जब तक condition true रहे।
उदाहरण: for as a while Loop
// 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() {
// Short declaration: create n and infer the type
n := 1
// Repeat while n < 100 is true
for n < 100 {
n *= 2
}
// Print the values followed by a newline
fmt.Println("first power of two >= 100:", n)
}
Login to try C/C++/Java/PHP code in the editor
Infinite Loops
तीनों clauses छोड़ देना एक infinite loop produce करता है, 'for { }', जो हमेशा चलता रहता है जब तक इसके अंदर कुछ break न करे या function से return न हो। यह form अक्सर servers या workers के लिए उपयोग होता है जिन्हें explicitly रुकने तक चलना चाहिए।
उदाहरण: Infinite Loops
// 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() {
// Short declaration: create count and infer the type
count := 0
// Loop forever until a break or return
for {
count++
if count == 3 {
break
}
}
// Print the values followed by a newline
fmt.Println("stopped at:", count)
}
Login to try C/C++/Java/PHP code in the editor
Nested for Loops
चूंकि for Go का इकलौता loop construct है, nested loops (एक loop के अंदर एक loop) एक for statement को दूसरे के अंदर रखकर लिखे जाते हैं, आमतौर पर grids या दो sequences के combinations पर iterate करने के लिए उपयोग होता है।
उदाहरण: Nested for Loops
// 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 := 1; i <= 3; i++ {
// Loop: repeat while the condition holds
for j := 1; j <= 2; j++ {
// Print using a format string
fmt.Printf("(%d,%d) ", i, j)
}
}
// Print the values followed by a newline
fmt.Println()
}
Login to try C/C++/Java/PHP code in the editor
- while या do-while keyword ढूंढना -- Go में सिर्फ for है, जिसे सिर्फ एक condition के साथ while-style में लिखा जा सकता है।
- यह भूल जाना कि loop variable loop तक scoped है और इसे अंदर बदलते हुए loop खत्म होने के बाद भी इसे मायने रखने के साथ बने रहने की उम्मीद करना।
- एक break condition या return के बिना एक infinite 'for {}' लिखना, गलती से program को hang करते हुए।
- Go में बिल्कुल एक looping keyword है, for, जिसे कई styles में उपयोग किया जाता है।
- classic three-part form है 'for init; condition; post { }'।
- 'for condition { }' दूसरी languages में while loop जैसा व्यवहार करता है।
- कोई clauses न होने पर 'for { }' एक explicit break या return तक हमेशा loop करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: