From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Iteration

Iteration

- In Go, there is one looping construct, the for loop. The for loop is very versatile, and can be used to implement patterns such as for, while, do-while, and do-until. For loops contain three parts: the pre-condition, the condition, and the post-condition. As we'll see, not all these sections are required when using for loops. The precondition, in this case, creating a new variable i and assigning it to the value of 0, happens only once before the first loop is executed, it is never run again. The condition is a Boolean statement that is executed before each loop. If the expression is true, the loop will execute, if false, the for loop will exit. The post-condition is executed after every loop. Here, for example, the post-condition is incrementing the variable i by 1. As you can see here from the output, the value 0 to 4 are printed. The value 0 is printed first because the pre-condition set the variable i = 0. It is not until the post-condition runs that the value of i gets…

Contents