Type Your Question


Go control flow statements (if, else, for)

 Tuesday, 18 March 2025
GOLANG

Go, like most programming languages, offers control flow statements to manage the execution order of your code. The primary control flow statements in Go are if, else, and for. Let's examine each in detail.

1. The if Statement

The if statement executes a block of code only if a specified condition is true. The syntax is straightforward:

go
if condition {
// Code to execute if the condition is true
}

The condition is an expression that evaluates to a boolean value (true or false). If the condition is true, the code within the curly braces {} is executed. Otherwise, it's skipped.

Example:

go
package main

import "fmt"

func main() {
x := 10
if x > 5 {
fmt.Println("x is greater than 5")
}
}

1.1 if-else Statement

To handle cases where you want different code to execute depending on whether the condition is true or false, you can use the if-else statement:

go
if condition {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

Example:

go
package main

import "fmt"

func main() {
x := 3
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is not greater than 5")
}
}

1.2 if-else if-else Statement

For more complex scenarios with multiple conditions, you can chain multiple if-else if blocks:

go
if condition1 {
// Code to execute if condition1 is true
} else if condition2 {
// Code to execute if condition1 is false and condition2 is true
} else if condition3 {
// Code to execute if condition1 and condition2 are false and condition3 is true
} else {
// Code to execute if all previous conditions are false
}

Example:

go
package main

import "fmt"

func main() {
score := 85

if score >= 90 {
fmt.Println("A")
} else if score >= 80 {
fmt.Println("B")
} else if score >= 70 {
fmt.Println("C")
} else {
fmt.Println("F")
}
}

1.3 Initialization in if Statements

Go allows you to declare variables within the condition of an if statement. This variable is only accessible within the scope of the if block (and any associated else blocks):

go
if x := someFunction(); x > 10 {
fmt.Println("x is greater than 10:", x)
} else {
fmt.Println("x is not greater than 10")
}

2. The for Statement

Go's for statement is a powerful and versatile loop. It's similar to the for loop in C and Java but more concise. It doesn't have a separate while loop construct. There are three main forms:

2.1 Traditional for loop

This is the most general form, resembling the traditional C-style for loop:

go
for initializer; condition; post {
// Code to execute repeatedly
}

initializer is executed once at the beginning of the loop. condition is checked before each iteration; if it's true, the loop continues. post is executed after each iteration. Any of these parts can be omitted.

Example:

go
package main

import "fmt"

func main() {
for i := 0; i < 5; i++ {
fmt.Println(i)
}
}

2.2 for loop as a while loop

By omitting the initializer and post parts, you create a while-like loop:

go
i := 0
for i < 5 {
fmt.Println(i)
i++
}

2.3 Infinite loop

Omitting all three parts creates an infinite loop (be careful!):

go
for {
// Code that will run forever unless explicitly broken
// (e.g., using a break statement)
}

2.4 break and continue Statements

The break statement exits the innermost loop it's in. The continue statement skips the rest of the current iteration and proceeds to the next one.

go
for i := 0; i < 10; i++ {
if i == 5 {
continue // Skip i = 5
}
if i == 8 {
break // Exit the loop when i = 8
}
fmt.Println(i)
}

3. switch Statement

While not explicitly requested, it's worth briefly mentioning Go's switch statement as another important control flow mechanism. It's a concise way to handle multiple conditional branches.

go
switch expression {
case value1:
// Code to execute if expression == value1
case value2:
// Code to execute if expression == value2
default:
// Code to execute if expression doesn't match any case
}

Go's switch has some unique features including implicit falls through if you don't use a break and the ability to use a conditional expression within a case:

go
switch x {
case 1, 2: //matches 1 or 2
fmt.Println("x is 1 or 2")
case 3:
fmt.Println("x is 3")
default:
fmt.Println("x is something else")

}

i := 10
switch {
case i > 10:
fmt.Println("i is greater than 10")
case i < 10:
fmt.Println("i is less than 10")
default:
fmt.Println("i is 10")
}



This comprehensive explanation covers the fundamental control flow statements in Go, providing examples and clarifications for each. Understanding these constructs is crucial for writing effective and readable Go programs.

Controlflow If Else For Loop 
 View : 41


Related


Translate : English Rusia China Jepang Korean Italia Spanyol Saudi Arabia

Technisty.com is the best website to find answers to all your questions about technology. Get new knowledge and inspiration from every topic you search.