xxxxxxxxxx
package main
import "fmt"
func main(){
//only one type of look in golang. A for loop. to make a while loop simple
//emit all statments but the bools
var x int = 10
for x > 0{
fmt.Println(x)//this will print all numbers from 10-1
x--//decrement the x to make it go down by one each loop
}
}
xxxxxxxxxx
// Program to print number from 1 to 5
package main
import "fmt"
func main(){
number := 1
// loop that runs infinitely
for {
// condition to terminate the loop
if number > 5 {
break;
}
fmt.Printf("%d\n", number);
number ++
}
}
here is a sample do while loop.
xxxxxxxxxx
for ok := true; ok; ok = condition {
work()
}
for {
work()
if !condition {
break
}
}
xxxxxxxxxx
package main
import "fmt"
func main() {
sum := 1
for sum < 10 {
fmt.Println(sum)
sum++
}
}
xxxxxxxxxx
C's while is spelled for in Go.
package main
import "fmt"
func main() {
sum := 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
}
xxxxxxxxxx
//In Go while is replaced by just using for
counter:=1
for counter < 10 {
//Do something
counter++
}