From 0d55b2e4908d3785f36a1339b2fe89b302a9f939 Mon Sep 17 00:00:00 2001 From: asandikci Date: Sun, 9 Jul 2023 22:03:02 +0300 Subject: [PATCH] For Loops and If --- 1flow/for.go | 33 +++++++++++++++++++++++++++++++++ 1flow/if.go | 24 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 1flow/for.go create mode 100644 1flow/if.go diff --git a/1flow/for.go b/1flow/for.go new file mode 100644 index 0000000..8b655f7 --- /dev/null +++ b/1flow/for.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("There is no parantheses in for loop unlike other languages") + fmt.Println("braces are necessary in for loop") + + /* SYNTAX: + for INIT; CONDITION; POST { + + } + */ + + sum := 0 + for i := 0; i < 10; i++ { + sum += i + } + fmt.Println(sum) + + fmt.Println("Init and post statements are optional\nSo you can use for loop as while in other languages") + + val := 100 + for val > 0 { + val -= 1 + } + fmt.Println(val) + + fmt.Println("break and continue keywords are same as C") + +} diff --git a/1flow/if.go b/1flow/if.go new file mode 100644 index 0000000..166294d --- /dev/null +++ b/1flow/if.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("No parantheses, braces required") + val := 1 + if val == 0 { + fmt.Println("1") + } else if val == 1 { + fmt.Println("2") + } else { + fmt.Println("3") + } + + fmt.Println("Also you can execute short statement before condition") + if val2 := 1; val2 > 0 { + fmt.Println(val2) + } + // fmt.Println(val2) // ERROR, out of scope + +}