github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/misc/tour/content/flowcontrol.article (about) 1 Flow control statements: for, if, else, switch and defer 2 Learn how to control the flow of your code with conditionals, loops, switches and defers. 3 4 The Go Authors 5 https://golang.org 6 7 * For 8 9 Go has only one looping construct, the `for` loop. 10 11 The basic `for` loop has three components separated by semicolons: 12 13 - the init statement: executed before the first iteration 14 - the condition expression: evaluated before every iteration 15 - the post statement: executed at the end of every iteration 16 17 The init statement will often be a short variable declaration, and the 18 variables declared there are visible only in the scope of the `for` 19 statement. 20 21 The loop will stop iterating once the boolean condition evaluates to `false`. 22 23 _Note_: Unlike other languages like C, Java, or Javascript there are no parentheses 24 surrounding the three components of the `for` statement and the braces `{`}` are 25 always required. 26 27 .play flowcontrol/for.go 28 29 * For continued 30 31 The init and post statement are optional. 32 33 .play flowcontrol/for-continued.go 34 35 * For is Go's "while" 36 37 At that point you can drop the semicolons: C's `while` is spelled `for` in Go. 38 39 .play flowcontrol/for-is-gos-while.go 40 41 * Forever 42 43 If you omit the loop condition it loops forever, so an infinite loop is compactly expressed. 44 45 .play flowcontrol/forever.go 46 47 * If 48 49 Go's `if` statements are like its `for` loops; the expression need not be 50 surrounded by parentheses `(`)` but the braces `{`}` are required. 51 52 .play flowcontrol/if.go 53 54 * If with a short statement 55 56 Like `for`, the `if` statement can start with a short statement to execute before the condition. 57 58 Variables declared by the statement are only in scope until the end of the `if`. 59 60 (Try using `v` in the last `return` statement.) 61 62 .play flowcontrol/if-with-a-short-statement.go 63 64 * If and else 65 66 Variables declared inside an `if` short statement are also available inside any 67 of the `else` blocks. 68 69 (Both calls to `pow` are executed and return before the call to `fmt.Println` 70 in `main` begins.) 71 72 .play flowcontrol/if-and-else.go 73 74 * Exercise: Loops and Functions 75 76 As a simple way to play with functions and loops, implement the square root function using Newton's method. 77 78 In this case, Newton's method is to approximate `Sqrt(x)` by picking a starting point _z_ and then repeating: 79 80 .image /content/img/newton.png 81 82 To begin with, just repeat that calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...). 83 84 Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small delta). See if that's more or fewer iterations. How close are you to the [[https://golang.org/pkg/math/#Sqrt][math.Sqrt]]? 85 86 Hint: to declare and initialize a floating point value, give it floating point syntax or use a conversion: 87 88 z := float64(1) 89 z := 1.0 90 91 .play flowcontrol/exercise-loops-and-functions.go 92 93 * Switch 94 95 You probably knew what `switch` was going to look like. 96 97 A case body breaks automatically, unless it ends with a `fallthrough` statement. 98 99 .play flowcontrol/switch.go 100 101 * Switch evaluation order 102 103 Switch cases evaluate cases from top to bottom, stopping when a case succeeds. 104 105 (For example, 106 107 switch i { 108 case 0: 109 case f(): 110 } 111 112 does not call `f` if `i==0`.) 113 114 #appengine: *Note:* Time in the Go playground always appears to start at 115 #appengine: 2009-11-10 23:00:00 UTC, a value whose significance is left as an 116 #appengine: exercise for the reader. 117 118 .play flowcontrol/switch-evaluation-order.go 119 120 * Switch with no condition 121 122 Switch without a condition is the same as `switch`true`. 123 124 This construct can be a clean way to write long if-then-else chains. 125 126 .play flowcontrol/switch-with-no-condition.go 127 128 * Defer 129 130 A defer statement defers the execution of a function until the surrounding 131 function returns. 132 133 The deferred call's arguments are evaluated immediately, but the function call 134 is not executed until the surrounding function returns. 135 136 .play flowcontrol/defer.go 137 138 * Stacking defers 139 140 Deferred function calls are pushed onto a stack. When a function returns, its 141 deferred calls are executed in last-in-first-out order. 142 143 To learn more about defer statements read this 144 [[https://blog.golang.org/defer-panic-and-recover][blog post]]. 145 146 .play flowcontrol/defer-multi.go 147 148 * Congratulations! 149 150 You finished this lesson! 151 152 You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].