If-Else Statement in Go

List Topics
February 15, 2025
No Comments
4 min read

What is If-Else?

The if-else statement in Go is used for decision-making. It allows a program to execute a block of code conditionally, based on whether a certain condition is true or false.

Types of If-Else Statements in Go

  1. Simple if statement
  2. if-else statement
  3. if-else if-else statement
  4. Short statement in if condition
  5. Nested if statement

1. Simple if Statement

The if statement checks a condition. If the condition is true, it executes the block of code inside {}. Otherwise, it skips that block.

Syntax:

Go
if condition {
    // Code to execute if condition is true
}

Example:

Go
package main

import "fmt"

func main() {
    num := 10
    if num > 0 {
        fmt.Println("The number is positive.")
    }
}

🔹 Output:

Bash
The number is positive.

👉 Explanation: Since num > 0 is true, the message is printed.

2. if-else Statement

The if-else statement is used when you need to execute one block of code if a condition is true and another block if the condition is false.

Syntax:

Go
if condition {
    // Code if condition is true
} else {
    // Code if condition is false
}

Example:

Go
package main

import "fmt"

func main() {
    num := -5
    if num > 0 {
        fmt.Println("The number is positive.")
    } else {
        fmt.Println("The number is not positive.")
    }
}

🔹 Output:

Bash
The number is not positive.

👉 Explanation: Since num > 0 is false, the else block executes.

3. if-else if-else Statement

The if-else if-else statement allows checking multiple conditions. The first condition that evaluates to true is executed, and the rest are skipped.

Syntax:

Go
if condition1 {
    // Code if condition1 is true
} else if condition2 {
    // Code if condition2 is true
} else {
    // Code if all conditions are false
}

Example:

Go
package main

import "fmt"

func main() {
    num := 0
    if num > 0 {
        fmt.Println("The number is positive.")
    } else if num < 0 {
        fmt.Println("The number is negative.")
    } else {
        fmt.Println("The number is zero.")
    }
}

🔹 Output:

Bash
The number is zero.

👉 Explanation: Since num is neither greater nor less than zero, the else block executes.

4. Short Statement in if Condition

In Go, you can declare a variable inside the if condition. This is useful when you only need the variable within the if block.

Syntax:

Go
if variable := value; condition {
    // Code if condition is true
}

Example:

Go
package main

import "fmt"

func main() {
    if num := 5; num%2 == 0 {
        fmt.Println("Even number")
    } else {
        fmt.Println("Odd number")
    }
}

🔹 Output:

Bash
Odd number

👉 Explanation: The variable num is declared inside if. Since 5%2 != 0, the else block executes.

5. Nested if Statement

An if statement inside another if statement is called nested if. This helps when multiple conditions depend on each other.

Example:

Go
package main

import "fmt"

func main() {
    num := 20
    if num > 0 {
        fmt.Println("Positive number")
        if num%2 == 0 {
            fmt.Println("Even number")
        }
    }
}

🔹 Output:

Bash
Positive number
Even number

👉 Explanation:

  • First, if num > 0 executes.
  • Then, inside it, if num%2 == 0 checks if num is even.

Conclusion

if is used for simple condition checking.
if-else lets you handle both true and false cases.
if-else if-else allows multiple condition checking.
✅ Short statements help declare variables inside if.
✅ Nested if is used when multiple conditions need to be checked sequentially.


If-Else স্টেটমেন্ট: বিস্তারিত ব্যাখ্যা

If-Else কী?

Go-তে if-else স্টেটমেন্ট ব্যবহার করে শর্ত অনুসারে প্রোগ্রাম চালানো যায়

If-Else-এর ধরন

  1. সাধারণ if স্টেটমেন্ট
  2. if-else স্টেটমেন্ট
  3. if-else if-else স্টেটমেন্ট
  4. সংক্ষিপ্ত if স্টেটমেন্ট
  5. নেস্টেড if স্টেটমেন্ট

1. সাধারণ if স্টেটমেন্ট

Go
if শর্ত {
    // যদি শর্ত সত্য হয়, এই কোড চলবে
}

উদাহরণ:

Go
num := 10
if num > 0 {
    fmt.Println("এই সংখ্যা ধনাত্মক।")
}

2. if-else স্টেটমেন্ট

Go
if শর্ত {
    // শর্ত সত্য হলে এটি চলবে
} else {
    // শর্ত মিথ্যা হলে এটি চলবে
}

উদাহরণ:

Go
num := -5
if num > 0 {
    fmt.Println("সংখ্যাটি ধনাত্মক।")
} else {
    fmt.Println("সংখ্যাটি ধনাত্মক নয়।")
}

3. if-else if-else স্টেটমেন্ট

Go
if শর্ত1 {
    // শর্ত1 সত্য হলে এই কোড চলবে
} else if শর্ত2 {
    // শর্ত2 সত্য হলে এই কোড চলবে
} else {
    // কোনো শর্তই সত্য না হলে এটি চলবে
}

উদাহরণ:

Go
num := 0
if num > 0 {
    fmt.Println("ধনাত্মক সংখ্যা।")
} else if num < 0 {
    fmt.Println("ঋণাত্মক সংখ্যা।")
} else {
    fmt.Println("সংখ্যাটি শূন্য।")
}

4. সংক্ষিপ্ত if স্টেটমেন্ট

Go
if ভেরিয়েবল := মান; শর্ত {
    // শর্ত সত্য হলে এটি চলবে
}

উদাহরণ:

Go
if num := 5; num%2 == 0 {
    fmt.Println("জোড় সংখ্যা")
} else {
    fmt.Println("বিজোড় সংখ্যা")
}

5. নেস্টেড if স্টেটমেন্ট

উদাহরণ:

Go
num := 20
if num > 0 {
    fmt.Println("ধনাত্মক সংখ্যা")
    if num%2 == 0 {
        fmt.Println("জোড় সংখ্যা")
    }
}

সারসংক্ষেপ

if শর্ত চেক করে।
if-else দুটি অপশন দেয়।
if-else if-else একাধিক শর্ত চেক করতে পারে।
✅ সংক্ষিপ্ত if ভেরিয়েবল ঘোষণা সহজ করে।
✅ নেস্টেড if একাধিক শর্ত চেক করার জন্য ভালো।

©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.