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

for Loop क्या है

for loop Go का यह कहने का तरीका है 'जब तक मैं रुकने को न कहूं यह बार-बार करो' -- और यही Go का इकलौता loop है।
Syntax
go
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

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 < 5; i++ {
		// Print the values followed by a newline
		fmt.Println("iteration", i)
	}
}

for as a while Loop

init और post clauses हटाकर सिर्फ एक condition रखना for को दूसरी languages में मिलने वाले while loop के बराबर बना देता है -- यह तब तक चलता रहता है जब तक condition true रहे।

उदाहरण: for as a while Loop

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() {
	// 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)
}

Infinite Loops

तीनों clauses छोड़ देना एक infinite loop produce करता है, 'for { }', जो हमेशा चलता रहता है जब तक इसके अंदर कुछ break न करे या function से return न हो। यह form अक्सर servers या workers के लिए उपयोग होता है जिन्हें explicitly रुकने तक चलना चाहिए।

Note: हमेशा एक infinite for loop को एक break, return, या os.Exit के साथ जोड़ें ताकि program का एक रास्ता बाहर हो।

उदाहरण: Infinite Loops

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() {
	// 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)
}

Nested for Loops

चूंकि for Go का इकलौता loop construct है, nested loops (एक loop के अंदर एक loop) एक for statement को दूसरे के अंदर रखकर लिखे जाते हैं, आमतौर पर grids या दो sequences के combinations पर iterate करने के लिए उपयोग होता है।

उदाहरण: Nested for Loops

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 := 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()
}
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. while या do-while keyword ढूंढना -- Go में सिर्फ for है, जिसे सिर्फ एक condition के साथ while-style में लिखा जा सकता है।
  2. यह भूल जाना कि loop variable loop तक scoped है और इसे अंदर बदलते हुए loop खत्म होने के बाद भी इसे मायने रखने के साथ बने रहने की उम्मीद करना।
  3. एक 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:

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.