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

if / else के Statements

एक if/else statement आपके program को एक decision लेने देता है, जैसे किसी fork में किस रास्ते पर चलना है यह चुनना।
Syntax
go
if condition {
    // body
} else if condition2 {
    // body
} else {
    // body
}

if init; condition {
    // body
}

Basic if / else

एक if statement सिर्फ तभी एक block चलाता है जब इसकी condition true evaluate हो, और एक optional else block अन्यथा चलता है। C या Java के विपरीत, condition को कभी parentheses में wrap नहीं किया जाता, और braces हमेशा ज़रूरी हैं, एक single statement के लिए भी।

Note: Go को कभी condition के चारों ओर parentheses नहीं चाहिए (या नहीं चाहते)।

उदाहरण: Basic if / else

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 age and infer the type
	age := 20
	// Check whether age >= 18
	if age >= 18 {
		// Print the values followed by a newline
		fmt.Println("adult")
	} else {
		// Print the values followed by a newline
		fmt.Println("minor")
	}
}

else if Chains

कई conditions को 'else if' के साथ chain किया जा सकता है, ऊपर से नीचे evaluate होते हुए जब तक कोई एक true न हो, कोई मेल न खाने पर एक आखिरी else पर गिरते हुए -- कई mutually exclusive cases handle करने के लिए उपयोगी।

उदाहरण: else if Chains

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 score and infer the type
	score := 72
	// Check whether score >= 90
	if score >= 90 {
		// Print the values followed by a newline
		fmt.Println("Grade A")
	} else if score >= 75 {
		// Print the values followed by a newline
		fmt.Println("Grade B")
	} else if score >= 60 {
		fmt.Println("Grade C")
	} else {
		fmt.Println("Grade F")
	}
}

if with an Init Statement

Go आपको condition से पहले, एक semicolon से अलग, एक छोटा statement चलाने देता है, जो सबसे अक्सर एक value assign करने और तुरंत इसे test करने के लिए उपयोग होता है -- एक value और एक error return करने वाले functions के साथ बहुत आम pattern।

Note: किसी if के init statement में declared variables सिर्फ उस if/else chain के अंदर दिखाई देते हैं।

उदाहरण: if with an Init Statement

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

// Import the fmt package
import "fmt"

// Define the function half
func half(n int) (int, bool) {
	// Check whether n%2 != 0
	if n%2 != 0 {
		// Send 0, false back to the caller
		return 0, false
	}
	// Send n / 2, true back to the caller
	return n / 2, true
}

// main is where the program starts running
func main() {
	// Check whether result, ok := half(10); ok
	if result, ok := half(10); ok {
		fmt.Println("half is:", result)
	} else {
		fmt.Println("not evenly divisible")
	}
}

Boolean Expressions

Conditions comparisons को && (and), || (or), और ! (not) के साथ combine करते हैं, बिल्कुल ज़्यादातर C-family languages की तरह, और Go इन operators को short-circuit करता है, result तय होते ही evaluation रोकते हुए।

उदाहरण: Boolean Expressions

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 age and infer the type
	age := 25
	// Short declaration: create hasID and infer the type
	hasID := true
	// Check whether age >= 18 && hasID
	if age >= 18 && hasID {
		// Print the values followed by a newline
		fmt.Println("entry allowed")
	}
}
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. condition को 'if (x > 5)' जैसे parentheses में wrap करना -- Go इसकी अनुमति देता है लेकिन idiomatic style हमेशा इन्हें छोड़ देती है।
  2. यह भूल जाना कि opening brace if/else के साथ उसी line पर होनी चाहिए -- Go का automatic semicolon insertion अन्यथा टूट जाता है।
  3. यह न समझना कि if के init statement में declared variable (if x := f(); x > 0) सिर्फ if/else chain तक scoped है, बाहर नहीं।
चैप्टर सारांश
  • if/else एक boolean condition के आधार पर branch करता है, condition के चारों ओर parentheses की ज़रूरत नहीं।
  • एक optional init statement condition से पहले चल सकता है, एक variable को पूरी if/else chain तक scope करते हुए।
  • else if chains कई conditions को साथ जोड़ती हैं।
  • Go में braces अनिवार्य हैं, कुछ C-family languages के विपरीत जो बिना उनके single-statement bodies की अनुमति देती हैं।
🔒

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.