Skip to content

Instantly share code, notes, and snippets.

@overloadedargs
Created April 21, 2020 12:41
Show Gist options
  • Save overloadedargs/606d9051e120380761cd92e1627242f0 to your computer and use it in GitHub Desktop.
Save overloadedargs/606d9051e120380761cd92e1627242f0 to your computer and use it in GitHub Desktop.
Showing the differences between loop types
package main
import "fmt"
func for_loop() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
func while_loop() {
sum := 0
for i:= 0; sum < 1000; i++ {
sum += i
}
fmt.Println(sum)
}
func do_while_loop() {
sum := 0
i := 0
for {
sum += i
i++
if sum > 1000 { break }
}
fmt.Println(sum)
}
func main() {
for_loop();
while_loop();
do_while_loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment