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
.
if
statementif-else
statementif-else if-else
statementif
conditionif
statementif
StatementThe if
statement checks a condition. If the condition is true
, it executes the block of code inside {}
. Otherwise, it skips that block.
if condition {
// Code to execute if condition is true
}
package main
import "fmt"
func main() {
num := 10
if num > 0 {
fmt.Println("The number is positive.")
}
}
🔹 Output:
The number is positive.
👉 Explanation: Since num > 0
is true
, the message is printed.
if-else
StatementThe 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.
if condition {
// Code if condition is true
} else {
// Code if condition is false
}
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:
The number is not positive.
👉 Explanation: Since num > 0
is false
, the else
block executes.
if-else if-else
StatementThe if-else if-else
statement allows checking multiple conditions. The first condition that evaluates to true
is executed, and the rest are skipped.
if condition1 {
// Code if condition1 is true
} else if condition2 {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
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:
The number is zero.
👉 Explanation: Since num
is neither greater nor less than zero, the else
block executes.
if
ConditionIn Go, you can declare a variable inside the if
condition. This is useful when you only need the variable within the if
block.
if variable := value; condition {
// Code if condition is true
}
package main
import "fmt"
func main() {
if num := 5; num%2 == 0 {
fmt.Println("Even number")
} else {
fmt.Println("Odd number")
}
}
🔹 Output:
Odd number
👉 Explanation: The variable num
is declared inside if
. Since 5%2 != 0
, the else
block executes.
if
StatementAn if
statement inside another if
statement is called nested if. This helps when multiple conditions depend on each other.
package main
import "fmt"
func main() {
num := 20
if num > 0 {
fmt.Println("Positive number")
if num%2 == 0 {
fmt.Println("Even number")
}
}
}
🔹 Output:
Positive number
Even number
👉 Explanation:
if num > 0
executes.if num%2 == 0
checks if num
is even.✅ 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.
Go-তে if-else
স্টেটমেন্ট ব্যবহার করে শর্ত অনুসারে প্রোগ্রাম চালানো যায়।
if
স্টেটমেন্টif-else
স্টেটমেন্টif-else if-else
স্টেটমেন্টif
স্টেটমেন্টif
স্টেটমেন্টif
স্টেটমেন্টif শর্ত {
// যদি শর্ত সত্য হয়, এই কোড চলবে
}
✅ উদাহরণ:
num := 10
if num > 0 {
fmt.Println("এই সংখ্যা ধনাত্মক।")
}
if-else
স্টেটমেন্টif শর্ত {
// শর্ত সত্য হলে এটি চলবে
} else {
// শর্ত মিথ্যা হলে এটি চলবে
}
✅ উদাহরণ:
num := -5
if num > 0 {
fmt.Println("সংখ্যাটি ধনাত্মক।")
} else {
fmt.Println("সংখ্যাটি ধনাত্মক নয়।")
}
if-else if-else
স্টেটমেন্টif শর্ত1 {
// শর্ত1 সত্য হলে এই কোড চলবে
} else if শর্ত2 {
// শর্ত2 সত্য হলে এই কোড চলবে
} else {
// কোনো শর্তই সত্য না হলে এটি চলবে
}
✅ উদাহরণ:
num := 0
if num > 0 {
fmt.Println("ধনাত্মক সংখ্যা।")
} else if num < 0 {
fmt.Println("ঋণাত্মক সংখ্যা।")
} else {
fmt.Println("সংখ্যাটি শূন্য।")
}
if
স্টেটমেন্টif ভেরিয়েবল := মান; শর্ত {
// শর্ত সত্য হলে এটি চলবে
}
✅ উদাহরণ:
if num := 5; num%2 == 0 {
fmt.Println("জোড় সংখ্যা")
} else {
fmt.Println("বিজোড় সংখ্যা")
}
if
স্টেটমেন্ট✅ উদাহরণ:
num := 20
if num > 0 {
fmt.Println("ধনাত্মক সংখ্যা")
if num%2 == 0 {
fmt.Println("জোড় সংখ্যা")
}
}
✅ if
শর্ত চেক করে।
✅ if-else
দুটি অপশন দেয়।
✅ if-else if-else
একাধিক শর্ত চেক করতে পারে।
✅ সংক্ষিপ্ত if
ভেরিয়েবল ঘোষণা সহজ করে।
✅ নেস্টেড if
একাধিক শর্ত চেক করার জন্য ভালো।