Created
April 21, 2020 12:41
-
-
Save overloadedargs/606d9051e120380761cd92e1627242f0 to your computer and use it in GitHub Desktop.
Showing the differences between loop types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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