{"id":2110,"date":"2020-05-07T00:54:28","date_gmt":"2020-05-06T19:24:28","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2110"},"modified":"2020-11-25T00:39:57","modified_gmt":"2020-11-24T19:09:57","slug":"for-loop-in-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/for-loop-in-golang\/","title":{"rendered":"For Loop in go (golang)"},"content":{"rendered":"\n
This is the\u00a0 chapter 10 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series<\/a><\/p>\n\n\n\n Next Tutorial<\/strong>\u00a0\u2013\u00a0 For Range loop<\/a> When it comes to loop, golang has:<\/p>\n\n\n\n In this tutorial, we will be learning about the for loop only. The while loop is missing from go but a while loop can be implemented using a for loop as we will see later in this tutorial.<\/p>\n\n\n\n Here is the sequence of execution of the three parts:<\/p>\n\n\n\n Some points to note about for loop:<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Output: <\/strong>Above program prints a infinite loop<\/p>\n\n\n\n break<\/strong> statement help exit out of the for loop. None of the statement after break is executed inside the for loop.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n continue <\/strong>statement help skip the current iteration of the for loop. None of the statement after the continue is executed and the execution reaches the start again with next iteration. The use case is when you want to only operate on certain elements of the for loop. <\/p>\n\n\n\n Let's see an exampleIn below program we only want to print non-multiples of 3.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n for loop can also be nested as well. See below example<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n The below code is an example that we can also have function call or assignment in init part too.<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n Go doesn't have the while<\/strong> keyword. Instead it has the for<\/strong> keyword only. However for<\/strong> keyword can be used to simulate the functionality same as while<\/strong>.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n This is all about for loop in golang. Please share feedback\/improvements\/mistakes in comments<\/p>\n\n\n\n Next Tutorial<\/strong>\u00a0\u2013\u00a0 For Range loop<\/a> This is the\u00a0 chapter 10 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series Next Tutorial\u00a0\u2013\u00a0 For Range loopPrevious Tutorial\u00a0\u2013 Constants…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-2110","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Previous Tutorial<\/strong>\u00a0\u2013 Constants<\/a><\/p>\n\n\n\n
Now let\u2019s check out the current tutorial. Below is the table of contents for current tutorial.<\/p>\n\n\n\nOverview<\/strong><\/h1>\n\n\n\n
for<\/strong> loop in GO basically has three parts as shown below in the format<\/p>\n\n\n\nfor init_part; condition_part; post_part {\n ...\n}<\/code><\/pre>\n\n\n\n
Examples<\/strong><\/h1>\n\n\n\n
Simple for loop<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n for i := 0; i < 5; i++ {\n fmt.Println(i)\n }\n}<\/code><\/pre>\n\n\n\n
0\n1\n2\n3\n4<\/code><\/pre>\n\n\n\n
For loop with only condition<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n i := 0\n for i < 5 {\n fmt.Println(i)\n i++\n }\n}<\/code><\/pre>\n\n\n\n
0\n1\n2\n3\n4<\/code><\/pre>\n\n\n\n
For Infinite loop<\/strong><\/h2>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"time\"\n)\n\nfunc main() {\n i := 0\n for {\n fmt.Println(i)\n i++\n time.Sleep(time.Second * 1)\n }\n}<\/code><\/pre>\n\n\n\n
0\n1\n2\n3\n4\n5\n.\n.<\/code><\/pre>\n\n\n\n
Break Statement in For loop<\/strong><\/h2>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n)\n\nfunc main() {\n i := 0\n for {\n fmt.Println(i)\n i++\n if i >= 5 {\n break\n }\n }\n}<\/code><\/pre>\n\n\n\n
0\n1\n2\n3\n4<\/code><\/pre>\n\n\n\n
Continue Statement in For loop<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n for i := 1; i < 10; i++ {\n if i%3 == 0 {\n continue\n }\n fmt.Println(i)\n }\n}<\/code><\/pre>\n\n\n\n
1\n2\n4\n5\n7\n8\n10<\/code><\/pre>\n\n\n\n
Nested For Loop<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n for i := 0; i < 3; i++ {\n fmt.Printf(\"Outer loop iteration %d\\n\", i)\n for j := 0; j < 2; j++ {\n fmt.Printf(\"i= %d j=%d\\n\", i, j)\n }\n }\n}<\/code><\/pre>\n\n\n\n
Outer loop iteration 0\ni= 0 j=0\ni= 0 j=1\nOuter loop iteration 1\ni= 1 j=0\ni= 1 j=1\nOuter loop iteration 2\ni= 2 j=0\ni= 2 j=1<\/code><\/pre>\n\n\n\n
Function Call and Assignment in Init part<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n i := 1\n \/\/Function call in the init part in for loop\n for test(); i < 3; i++ {\n fmt.Println(i)\n }\n\n \/\/Assignment in the init part in for loop\n for i = 2; i < 3; i++ {\n fmt.Println(i)\n }\n}\nfunc test() {\n fmt.Println(\"In test function\")\n}<\/code><\/pre>\n\n\n\n
In test function\n1\n2\n2<\/code><\/pre>\n\n\n\n
Implementing while loop using for loop<\/strong><\/h2>\n\n\n\n
for<\/strong> loop can be implemented to behave the same as while<\/strong> if initialization_part<\/strong> and increment_part<\/strong> can be skipped. Here is an example:<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n i := 1\n for i <= 5 {\n fmt.Println(i)\n i++\n }\n}<\/code><\/pre>\n\n\n\n
1\n2\n3\n4\n5<\/code><\/pre>\n\n\n\n
Conclusion<\/strong><\/h1>\n\n\n\n
Previous Tutorial<\/strong>\u00a0\u2013 Constants<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"