For Loops and If
This commit is contained in:
parent
04ad377375
commit
0d55b2e490
2 changed files with 57 additions and 0 deletions
33
1flow/for.go
Normal file
33
1flow/for.go
Normal file
|
@ -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")
|
||||||
|
|
||||||
|
}
|
24
1flow/if.go
Normal file
24
1flow/if.go
Normal file
|
@ -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
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue