Every thing about For Loop In Golang – 2019
Check This Previous blog post written by Sagar Jaybhay for better understanding.
- Part 1: https://sagarjaybhay.net/google-go-tutorial-by-sagar-jaybhay-blog-1/
- Part 2: https://sagarjaybhay.net/packages-and-golang-command-part-2/
- Part 3: https://sagarjaybhay.net/detail-datatypes-in-golang-you-should-know-2019/
- Part 4:https://sagarjaybhay.net/tutorial-go-language-constant-in-golang-2019/
- Part 5:https://sagarjaybhay.net/control-flow-statement-in-golang-2019/
For loop in golang
In a go language, there is only one loop that for similar to C#, Java and other languages but there is no foreach loop in golang.
For loop composed of 3 components which are separated by a semicolon.
for i := 0; i < 10; i++
- First statement: which is called init statement. It is executed before the first iteration.
- Second statement: it is called condition expression and it is evaluated before every iteration.
- Third statement: it is also called a post statement and it is executed at the end of every iteration.
The init statement is often a short variable declaration but if you initialize your variable above the for loop anywhere and you want to use this variable you can use this and no need to write in that init statement leave it blank.
package main func main() { println("Hello Sagar Jaybhay") for i = 0; i < 10; i++ { println("value of i ", i) } }
In the above code, in for loop init statement we assign i=0 which not work when you try to run this program it will throw below error.
PS D:\Target Dec-2019\GoProgramming\GoLanguageLab> go run forLoop.go
# command-line-arguments
.\forLoop.go:6:6: undefined: i
.\forLoop.go:7:26: undefined: i
PS D:\Target Dec-2019\GoProgramming\GoLanguageLab>

See highlighted syntax in above image, this program run perfectly fine and no error occurred. Code for this is below
package main func main() { println("Hello Sagar Jaybhay") for i := 0; i < 10; i++ { println("value of i ", i) } }
For loop in golang without init statement and without post conditional statement
package main import "fmt" func main() { sum: = 1 for; sum < 1000; { sum += sum } fmt.Println(sum) }
If you omit the loop condition and never increment or decrement variable or simple for loop which runs forever until your CPU memory exhausted.
func main() { for { } }
For Loop With Condition Only:
In this case for loop works like a while loop like other languages
package main func main() { println("I am In Main Function") i := 0 for i < 10 { println("value of i: ", i) i++ } }
See above code in this we only use condition which checks I less than 10 or not and pre initialization of variable before loops start and post-increment is done inside the for a loop.
For loop with break and continue:
In this below code we are using for loop with break and continue statement. The use of a continue statement is that it continues the loop means what when the condition is full fill and our program thread finds continue statement the control bypass the next all statement and move the pointer to loop start.
Break: In this program, we have the condition if the value of i going above 30 then it will break. What it breaks? It break the for loop and exit from there.
The below program will print the odd numbers only because in if condition we check the even number means the remainder is 0 when we divide by 2 and then we use continue to start again from start.
package main func main() { println("I am In Main Function") i:= 0 for { i++ if i%2 == 0 { continue } println("val of i :=> ", i) if i >= 30 { break } } }
GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab