# Go Control Flow
###### tags: `golang`
# Operators
Before seeing the Control Flow, we will take some minutes to see the different operators.
## Comparison Operators
As other languages, Go also use **>, <, <=, >=, ==, !=** to compare.
```go=
speed := 100 // #1
// speed := 10 // #2
fast := speed >= 80
slow := speed < 20
fmt.Printf("fast's type is %T\n", fast)
fmt.Printf("going fast? %t\n", fast)
fmt.Printf("going slow? %t\n", slow)
fmt.Printf("is it 100 mph? %t\n", speed == 100)
fmt.Printf("is it not 100 mph? %t\n", speed != 100)
//Print Result:
//fast's type is bool
//going fast? true
//going slow? false
//is it 100 mph? true
//is it not 100 mph? false
```
## Logical Operators
As for this operator we also use && (AND), ||(OR)
# If Statement
>Unlike Java or Kotlin, the condition in if statement in Go does not need to be explicited inside **parenthesis()**. [color=red]
**Example:**
```go=
if true {
fmt.Println("true")
} else {
fmt.Println("false")
}
```
[For more information on Golang If Statements, Click Here.](/giPezbFJRUul6j8IO5wZ7A)
# For Statement
There is no **while** nor **until** statement in Go. Only **For statement** is available for loop in go.
```go=
var sum int
for i:=0; i < 5; i++ {
sum += i
fmt.Println("Sum: ", sum)
}
```
## Range Loops
```go=
words := strings.Fields(
"Dog Rabbit Elephant",
)
for i, v := range words {
fmt.Printf("#%-2d: %q\n",
i+1, v,
)
}
//Print Result
// #1 : "Dog"
// #2 : "Rabbit"
// #3 : "Elephant"
```
**Range with one variable**
Unlike above example, when using one variable, it means that this one represents the order (the loop number it is running).
Like in below example, we might use this kind when using multidimensional array.
```go=
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
//Print all numbers
for line := range digits[0] {
for digit := range digits {
fmt.Print(digits[digit][line], "⬜⬜⬜")
}
fmt.Println()
}
```

## Nested Loops
## Normal && Label - Break and Continue
### Normal Mode
**Break**
As other languages, when we put a **break** inside a loop, it just gets out of the nearest loop and continue with the outer loop. Or else it will continue with the code flow we have after the loop.
```go=
for {
if condition {
break
}
}
...will continue here after break
```
**Continue**
By using **continue**, you will skip the code below and keep running the remaining loop.
```go=
for i:=0; i < 50; i++ {
//(2) And will keep running here again after i++
if i % 2 == 0 {
continue
}
//(1) When continue is used, the code here will be skipped
}
```
### Label Mode
It is quite different from **Normal Break and Continue**, because the original ones would just affect the loop they are in, but if we label them, we can just state which loop we want to affect.
**Labeled Break**
By using a **name + :** we can state a label, which goes before the loop.
By looking in the example below, we can see that we have nested loops. If we do normal break inside the nested one, it would just break that one, but in this case we stated **break queries**, meaning it will just end the outer loop and continue with our project code.
>go run main.go Dog Rabbit
>//Printed Answer
>#1 : "Dog"
>[color=red]

**Labeled Continue**
Similar to **Labeled Break**, **Labeled Continue** will continue from the for loop where it is labeled. It can be its parent loop like in the example shown below.

**Label Break in Switch**
We can also use labeled break in switch inside loop.
**Example:**

## Random
We use a package called **rand** to get Random numbers.
