For Loop In 'Go'(Learn 'Go' - Part 11)

in #programming7 years ago

In Go, we use the 'For' statement to repeat a block of statements multiple times. Unlike other languages, Go has just one type of loop, i.e., 'for' and we can use it in a variety of ways.

<p dir="auto">  <p dir="auto">For Instance, <p dir="auto">  <p dir="auto">package main <p dir="auto">import "fmt" <p dir="auto">func main(){ <pre><code>i := 1 for i <= 10{ fmt.Println(i) i = 1 + 1 } <p dir="auto">} <p dir="auto"> <br />   <p dir="auto">In this program, we have created a variable called 'i' to store the number we want to print. After that, we have created a for loop with a conditional expression to check whether the value of 'i' is less than or equal to '10'. After that, we have used the fmt.Println statement to print the value of 'i' and finally adding 1 to the value of 'i' before completing the loop. <p dir="auto">  <p dir="auto">As long as the value of 'i' will be less than or equal to 10, the loop will continue. As we can clearly predict, the loop will continue until 10 and at each increment it will print the value. So, it will print numbers 1 to 10 and after that the loop will end. <p dir="auto">  <p dir="auto">You can also write the same program in a simpler way. <p dir="auto">func main(){ <pre><code>for i := 1; 1 <= 10; i++ {fmt.Println(i) } <p dir="auto">} <p dir="auto">We can use '++' to increment and '--' operators to decrement the values by 1 in our programs. <p dir="auto"> <br />  <br />   <p dir="auto"><strong>Previous Posts In The Series <p dir="auto"> <br />   <p dir="auto"><a href="https://steemit.com/programming/@technological/introduction-to-go-programming-language-part-1-learn-go" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Introduction To 'Go' Programming Language(Learn 'Go' - Part 1) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/25-basic-keywords-of-the-go-programming-language-part-2-learn-go" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">25 Basic Keywords Of The Go Programming Language (Learn 'Go' - Part 2) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/how-to-set-the-go-programming-environment-on-your-system" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">How To Set The Go Programming Environment On Your System?(Learn 'Go' - Part 3) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/create-your-first-program-in-go-language-learn-go-part-4" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Create Your First Program In Go Language (Learn 'Go' - Part 4) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/strings-in-go-learn-go-part-5" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Strings In 'Go'(Learn 'Go' - Part 5) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/booleans-in-go-learn-go-part-6" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Booleans In 'Go'(Learn 'Go' - Part 6) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/numbers-in-go-learn-go-part-7" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Numbers In 'Go'(Learn 'Go' - Part 7) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/variables-in-go-learn-go-part-8" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Variables In 'Go'(Learn 'Go' - Part 8) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/constants-in-go-learn-go-part-9" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Constants In 'Go'(Learn 'Go' - Part9) <p dir="auto">  <p dir="auto"><a href="https://steemit.com/programming/@technological/multiple-variables-in-go-learn-go-part10" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Multiple Variables In 'Go'(Learn 'Go' - Part10) <hr /> <p dir="auto"> <br />   <p dir="auto"><strong>Upcoming Posts <p dir="auto">  <p dir="auto"><strong>'If' Statement In Go(Learn 'Go' - Part 12)