{"id":163,"date":"2024-09-10T14:43:39","date_gmt":"2024-09-10T07:43:39","guid":{"rendered":"https:\/\/gotutorial.org\/?page_id=163"},"modified":"2024-09-26T07:57:02","modified_gmt":"2024-09-26T00:57:02","slug":"go-for-loop","status":"publish","type":"page","link":"https:\/\/www.gotutorial.org\/go-tutorial\/go-for-loop\/","title":{"rendered":"Go for loop"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use a Go for loop to repeatedly run a code block.<\/p>\n\n\n\n<p>The <code>for<\/code> loop statement allows you to run a block of code repeatedly. There are some variants of the <code>for<\/code> loop statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go for loop statement<\/h2>\n\n\n\n<p>Here&#8217;s the basic syntax of the <code>for<\/code> loop statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">for<\/span> initialization; condition; post {\n    <span class=\"hljs-comment\">\/\/ loop body<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>for<\/code> loop consists of three components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>initialization<\/code> statement: Go executes this statement once at the beginning of the loop. Typically, you use this part to declare and initialize a loop control variable.<\/li>\n\n\n\n<li><code>condition<\/code>: Go evaluates the <code>condition<\/code> expression before each iteration of the loop. If the condition is true, Go executes the loop body. If it is false, Go terminates the loop. The condition determines whether the loop continues or not.<\/li>\n\n\n\n<li><code>post<\/code> statement: Go executes the <code>post<\/code> statement at the end of each iteration, after executing the loop body. Typically, you use the post statement to update the loop control variable declared in the initialization statement.<\/li>\n<\/ul>\n\n\n\n<p>The following flowchart illustrates how the Go for loop works:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/gotutorial.org\/wp-content\/uploads\/2024\/09\/go-for-loop.svg\" alt=\"Go for loop\" class=\"wp-image-442\"\/><\/figure>\n\n\n\n<p>For example, the following shows how to use the basic for loop statement to display 4 numbers from 1 to 4 on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    <span class=\"hljs-keyword\">for<\/span> i := <span class=\"hljs-number\">1<\/span>; i &lt; <span class=\"hljs-number\">5<\/span>; i++ {\n        fmt.Println(i)\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1\n2\n3\n4<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>. Initialization: Declare the loop control variable i and initialize its value to 1: <code>i := 1<\/code><\/li>\n\n\n\n<li><strong>Step 2<\/strong>. Condition: Evaluate the condition <code>i &lt; 5<\/code>. If it is true, execute the loop body, otherwise, terminate it.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>. Execute loop body: Print the current value of the loop control variable (<code>i<\/code>)<\/li>\n\n\n\n<li><strong>Step 4<\/strong>. Post statement:  Increment <code>i<\/code> by one after each iteration.<\/li>\n\n\n\n<li><strong>Step 5<\/strong>. Repeat the steps 2 to 4 until the condition <code>i &lt; 5<\/code> evaluates to <code>false<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Go does not support <a href=\"https:\/\/www.javascripttutorial.net\/javascript-while-loop\/\" target=\"_blank\" rel=\"noreferrer noopener\">while<\/a> and <a href=\"https:\/\/www.javascripttutorial.net\/javascript-do-while\/\" target=\"_blank\" rel=\"noreferrer noopener\">do while<\/a> loop in other programming languages such as Java and C#. Instead, you can use the <code>for<\/code> loop as the <code>while<\/code> and <code>do while<\/code> loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using for loop as a while loop<\/h2>\n\n\n\n<p>The following syntax allows you to execute a block of code as long as a condition is true:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">for<\/span> condition {\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s like a <code>while<\/code> loop in other programming languages such as <a href=\"https:\/\/www.zentut.com\/java-tutorial\/java-while-loop\/\">Java<\/a> or <a href=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-while\/\">C#<\/a>.<\/p>\n\n\n\n<p>The following flowchart illustrates how the for condition works:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/gotutorial.org\/wp-content\/uploads\/2024\/09\/go-do-while-loop.svg\" alt=\"Go do while loop\" class=\"wp-image-441\"\/><\/figure>\n\n\n\n<p>For example, the following uses a <code>for<\/code> loop to display five numbers from 1 to 4 on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    index := <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-keyword\">for<\/span> index &lt; <span class=\"hljs-number\">5<\/span> {\n        fmt.Println(index)\n        index++\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare a variable <code>index<\/code> and initialize its value to 1.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">index := <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, execute a block of code within the curly braces (<code>{}<\/code>) as long as the expression <code>index &lt; 5<\/code> is true:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">for<\/span> index &lt; <span class=\"hljs-number\">5<\/span> {\n   <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, display the value of the index and increase its value by one in each iteration:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">fmt.Println(index)\nindex++<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the index is 5, the condition <code>index &lt; 5<\/code> becomes false, the loop is terminated and the control is passed to the statement after the <code>for<\/code> loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Infinite loops<\/h2>\n\n\n\n<p>If you omit the condition, you&#8217;ll have an infinite loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">for<\/span> {\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To exit a loop based on a condition, you can use the <code>break<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">for<\/span> {\n   <span class=\"hljs-keyword\">if<\/span> condition {\n       <span class=\"hljs-keyword\">break<\/span>\n   }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to use a <code>for<\/code> loop without a condition to display five numbers from 1 to 5 on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    index := <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-keyword\">for<\/span> {\n        fmt.Println(index)\n        index++\n        <span class=\"hljs-keyword\">if<\/span> index &gt; <span class=\"hljs-number\">5<\/span> {\n            <span class=\"hljs-keyword\">break<\/span>\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare a variable <code>index<\/code> and initialize its value to 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">index := <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, display the <code>index<\/code> and increase the <code>index<\/code>&#8216;s value one in each iteration:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">fmt.Println(index)\nindex++<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, exit the loop if the index is greater than 5.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">if<\/span> index &gt; <span class=\"hljs-number\">5<\/span> {\n    <span class=\"hljs-keyword\">break<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After five iterations, the <code>index<\/code> is 6, which makes the expression <code>index &gt; 5<\/code> true, and the <code>break<\/code> statement is reached which terminates the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>for<\/code> loop to execute a block of code a specified number of times.<\/li>\n\n\n\n<li>Use the <code>for condition {}<\/code> to execute a code block repeatedly as long as a condition is true.<\/li>\n\n\n\n<li>Use the <code>for {}<\/code> to create an infinite loop.<\/li>\n\n\n\n<li>Use the <code>break<\/code> statement to terminate a loop immediately.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t<div class=\"wth-thumbs\">\n\t\t<button\n\t\t\tdata-post=\"163\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-for-loop\/\"\n\t\t\tdata-post-title=\"Go for loop\"\n\t\t\tdata-response=\"1\"\n\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t<\/button>\n\n\t\t<button\n\t\t\tdata-response=\"0\"\n\t\t\tdata-post=\"163\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-for-loop\/\"\n\t\t\tdata-post-title=\"Go for loop\"\n\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t<\/button>\n\t<\/div>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use a Go for loop to repeatedly run a code block. The for loop statement allows you to run a block of code repeatedly. There are some variants of the for loop statement. Basic Go for loop statement Here&#8217;s the basic syntax of the for loop [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":10,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-163","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":5,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/163\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/163\/revisions\/444"}],"up":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/media?parent=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}