Presentation is loading. Please wait.

Presentation is loading. Please wait.

Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }

Similar presentations


Presentation on theme: "Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }"— Presentation transcript:

1 Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }

2 Golang Control Statements if var x bool false if x { … } var s string = ”efg” if s > ”abc” //true If s >= ”fgh” //false

3 Golang Control Statements if Exercise Create variables to hold todays temperature and age write code to print one of the following if age is below 10 and temperature is below 48.4, “Hot chocolate is served” if age is 18+ and less than 60 and temperature is below 48.4, “Coffee is server” for all others “Hot soup is served” Test your logic for all possible combinations

4 Golang Control Statements for for i:=0; i<10; i++ { … } for ;; { // or for { … break; } break lets you exit a for loop continue starts the next iteration of the for loop from the top

5 Golang Control Statements for Exercise Write a for loop to print first 10 positive number or first 10 even positive numbers or first 10 prime numbers

6 Golang Control Statements for for key,value := range map { … } for index,value := range array { … } for inderOrKey := range X { … } //you can drop value in a map or just iterate a string

7 Golang Control Statements for Exercise create a table of player vs points from the map exercise using for loop with range print… Player Joe scored 22 points Player Alex scored 48 points Player Gary scored 12 points Player Frank scored 32 points Player Mike scored 24 points

8 Golang Write a program to reverse and print a string var a string = “gnaloG gninrael ot emocleW” Use for loops with range and multi-variate assignments.

9 Golang Control Statements switch Switch allows to run a specified block of statements based on a value Golang switch does not need a explicit break var a int = 8 switch a%5 { case 0 : fmt.Println( “a 5 multiple”) case 4 : fmt.Println( “top”) case 1 : fmt.Println( “bottom”) default : fmt.Println( “somewhere”) }

10 Golang Control Statements switch Switch work on character and strings switch c { case '&': esc = "&" case '\'': esc = "&apos;" case '<': esc = "<" case '>': esc = ">" case '"': esc = """ default: panic("unrecognized escape character") }

11 Golang Control Statements switch Fallthrough allows switch to execute the next statement var a int = 8 switch a%5 { case 0 : fmt.Println( “a 5 multiple”) case 4 : fmt.Println( “top”) fallthrough case 1 : fmt.Println( “bottom”) default : fmt.Println( “somewhere”) }

12 Golang Control Statements switch Special use of switch to detect type var a int = 8 Switch a.(type) { case float64 : fmt.Println( “float”) case int : fmt.Println( “integer”) case char : fmt.Println( “char”) default : fmt.Println( “unknown”) }

13 Golang Builtin functions new make len append panic len(“abc”) a := []string{"a","b"} a = append(a,"c") fmt.Println(len(a),a)

14 Golang Homework: 1.Sort an array of integer in ascending order 2.Create a table of persons with list of hobbies, print the hobby and the list of persons who has the hobby next to it.


Download ppt "Golang Control Statements if if x < 0 { … } else { … } if v,err:=eval(me);v > 0 && err != nil { … }"

Similar presentations


Ads by Google