{"id":162,"date":"2026-07-07T20:29:05","date_gmt":"2026-07-07T20:29:05","guid":{"rendered":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/"},"modified":"2026-07-07T20:38:07","modified_gmt":"2026-07-07T20:38:07","slug":"05-go-control-flow","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","title":{"rendered":"Go Control Flow \u2013 if, else, switch, and for Loops"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 2, 2026.<\/strong> Refreshed for Go 1.22+ and current SEO best practices.<\/p>\n<\/div>\n<p>In this lesson we cover <strong>Go control flow<\/strong> \u2014 how programs make decisions and repeat work. You will learn <strong>go if else for loop<\/strong> patterns that appear in every real Go codebase: branching with <code>if<\/code> and <code>switch<\/code>, and iterating with <code>for<\/code> in its classic, while-style, and <code>range<\/code> forms. If you completed <a href=\"https:\/\/www.kindsonthegenius.com\/go\/04-go-variables-and-types\/\">Lesson 4 \u2013 Variables and Types<\/a>, you already know how to store data; now we act on it.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20134 of this series, especially <a href=\"https:\/\/www.kindsonthegenius.com\/go\/04-go-variables-and-types\/\">Variables and Types<\/a> and <a href=\"https:\/\/www.kindsonthegenius.com\/go\/03-go-basic-syntax\/\">Basic Syntax<\/a>. <strong>Estimated time:<\/strong> 40\u201350 minutes.<\/p>\n<h4 id=\"if-else\">1. if and else Statements<\/h4>\n<p>The <code>if<\/code> statement runs a block when a condition is true. Go requires parentheses around the condition \u2014 unlike C \u2014 but <strong>does not<\/strong> require parentheses around the condition expression itself:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tscore := 85\n\n\tif score >= 90 {\n\t\tfmt.Println(\"Grade: A\")\n\t} else if score >= 80 {\n\t\tfmt.Println(\"Grade: B\")\n\t} else {\n\t\tfmt.Println(\"Grade: C or below\")\n\t}\n}\n<\/code><\/pre>\n<p>Key rules: the opening brace <code>{<\/code> must be on the same line as <code>if<\/code> (Go enforces this). Conditions must be boolean expressions \u2014 Go will not treat <code>0<\/code> or <code>\"\"<\/code> as false the way some languages do. This strictness prevents subtle bugs and makes intent obvious when you read code.<\/p>\n<h4 id=\"if-short\">2. if with a Short Statement<\/h4>\n<p>Go allows a short statement before the condition, separated by a semicolon. The variable declared in that statement is scoped only to the <code>if<\/code> block (and its <code>else<\/code> branches):<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\nfunc main() {\n\tx := 16.0\n\n\tif root := math.Sqrt(x); root > 3 {\n\t\tfmt.Printf(\"Square root %.2f is greater than 3\\n\", root)\n\t} else {\n\t\tfmt.Printf(\"Square root %.2f is 3 or less\\n\", root)\n\t}\n\t\/\/ root is not visible here\n}\n<\/code><\/pre>\n<p>This pattern is idiomatic when you need a temporary value only for the decision \u2014 for example, parsing input or calling a function that returns a result and an error. You will see <code>if err := doSomething(); err != nil { ... }<\/code> throughout Go code; we explore that error-handling style more when we cover functions.<\/p>\n<h4 id=\"switch\">3. switch Statements<\/h4>\n<p><code>switch<\/code> compares a value against multiple cases. Unlike C, Go cases do not fall through unless you use <code>fallthrough<\/code>:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tday := \"Wednesday\"\n\n\tswitch day {\n\tcase \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\":\n\t\tfmt.Println(\"Weekday\")\n\tcase \"Saturday\", \"Sunday\":\n\t\tfmt.Println(\"Weekend\")\n\tdefault:\n\t\tfmt.Println(\"Unknown day\")\n\t}\n}\n<\/code><\/pre>\n<p>Go also supports <strong>switch without a tag<\/strong> \u2014 each case is a boolean expression, useful as a cleaner alternative to long <code>if else<\/code> chains:<\/p>\n<pre><code class=\"language-go\">score := 72\n\nswitch {\ncase score >= 90:\n\tfmt.Println(\"Excellent\")\ncase score >= 70:\n\tfmt.Println(\"Good\")\ndefault:\n\tfmt.Println(\"Keep practicing\")\n}\n<\/code><\/pre>\n<p>Switch cases can use constants, strings, integers, and more. Because there is no automatic fall-through, you rarely need <code>break<\/code> \u2014 one less source of errors compared to C-style switches.<\/p>\n<h4 id=\"for-classic\">4. Classic for Loop<\/h4>\n<p>Go has one loop keyword: <code>for<\/code>. The classic three-part form mirrors C:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tsum := 0\n\tfor i := 1; i <= 5; i++ {\n\t\tsum += i\n\t}\n\tfmt.Println(\"Sum 1..5 =\", sum) \/\/ 15\n}\n<\/code><\/pre>\n<p>The init statement, condition, and post statement are all optional. An infinite loop is written as <code>for { }<\/code> \u2014 use <code>break<\/code> inside to exit. Unlike Python's <code>for i in range(n)<\/code>, Go's C-style loop gives you full control over the counter variable and step size.<\/p>\n<h4 id=\"for-while\">5. While-Style for Loop<\/h4>\n<p>Go has no <code>while<\/code> keyword. A condition-only <code>for<\/code> loop acts like <code>while<\/code> in other languages:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tn := 1\n\tfor n < 100 {\n\t\tn *= 2\n\t}\n\tfmt.Println(\"First power of 2 >= 100:\", n) \/\/ 128\n}\n<\/code><\/pre>\n<p>This form is common when you do not know how many iterations you need in advance \u2014 reading from a buffer, retrying a network call, or processing until a sentinel value appears. Combine with <code>break<\/code> and <code>continue<\/code> to skip the rest of an iteration or exit the loop early.<\/p>\n<h4 id=\"for-range\">6. for range Loop<\/h4>\n<p>The <code>range<\/code> keyword iterates over slices, arrays, strings, maps, and channels. For a slice or array, you get an index and a copy of each element:<\/p>\n<pre><code class=\"language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tlanguages := []string{\"Go\", \"Python\", \"Rust\"}\n\n\tfor index, lang := range languages {\n\t\tfmt.Printf(\"%d: %s\\n\", index, lang)\n\t}\n\n\t\/\/ Index only \u2014 discard value with _\n\tfor i := range languages {\n\t\tfmt.Println(i, languages[i])\n\t}\n}\n<\/code><\/pre>\n<p>When ranging over a string, the index is a byte offset and the value is a <code>rune<\/code> (Unicode code point). For maps, <code>range<\/code> yields key-value pairs in random order. We cover slices and maps in depth in later lessons; for now, treat <code>range<\/code> as the idiomatic way to visit every item in a collection.<\/p>\n<h4 id=\"control-summary\">7. Choosing the Right Construct<\/h4>\n<p>Use this quick reference when deciding which control structure fits:<\/p>\n<table>\n<thead>\n<tr>\n<th>Goal<\/th>\n<th>Construct<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Two-way branch<\/td>\n<td><code>if else<\/code><\/td>\n<td><code>if x > 0 { ... } else { ... }<\/code><\/td>\n<\/tr>\n<tr>\n<td>Scoped temp before test<\/td>\n<td><code>if<\/code> short statement<\/td>\n<td><code>if v := f(); v > 0 { ... }<\/code><\/td>\n<\/tr>\n<tr>\n<td>Many discrete values<\/td>\n<td><code>switch<\/code><\/td>\n<td><code>switch day { case \"Mon\": ... }<\/code><\/td>\n<\/tr>\n<tr>\n<td>Counted iteration<\/td>\n<td>Classic <code>for<\/code><\/td>\n<td><code>for i := 0; i &lt; n; i++ { ... }<\/code><\/td>\n<\/tr>\n<tr>\n<td>Condition-driven loop<\/td>\n<td>While-style <code>for<\/code><\/td>\n<td><code>for condition { ... }<\/code><\/td>\n<\/tr>\n<tr>\n<td>Collection iteration<\/td>\n<td><code>for range<\/code><\/td>\n<td><code>for i, v := range items { ... }<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Go favors simplicity: one loop keyword, explicit conditions, and no hidden truthiness. If you learned control flow in <a href=\"https:\/\/www.kindsonthegenius.com\/python\/\">Python<\/a>, notice that Go's <code>for range<\/code> is the closest equivalent to <code>for item in collection<\/code>, while counted loops use the three-part <code>for<\/code> form instead of <code>range(n)<\/code>.<\/p>\n<h4 id=\"next\">8. Next Steps<\/h4>\n<p>You now understand <strong>go if else for loop<\/strong> control flow: branching, switch, and every form of the <code>for<\/code> loop. In the next lesson we cover <strong>functions<\/strong> \u2014 how to organize code into reusable blocks with parameters, return values, and <code>defer<\/code>.<\/p>\n<p>Continue the series: <a href=\"https:\/\/www.kindsonthegenius.com\/go\/04-go-variables-and-types\/\">Lesson 4 \u2013 Variables and Types<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/go\/06-go-functions\/\">Lesson 6 \u2013 Functions<\/a><\/p>\n<p>Browse all tutorials: <a href=\"https:\/\/www.kindsonthegenius.com\/content-directory\/\">Content Directory<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/go\/\">Go Programming Hub<\/a><\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>Does Go have a while loop?<\/strong><br \/>\nNo. Use <code>for condition { }<\/code> without init or post statements. It behaves exactly like a <code>while<\/code> loop in other languages.<\/p>\n<p><strong>Do switch cases fall through in Go?<\/strong><br \/>\nNo, by default. Each case runs only its own block and then exits the switch. Use <code>fallthrough<\/code> explicitly if you truly need C-style fall-through \u2014 which is rare.<\/p>\n<p><strong>Can I modify the loop variable in a range loop?<\/strong><br \/>\nThe range value is a <em>copy<\/em> of each element. Changing it does not update the underlying slice. Use the index form <code>for i := range s { s[i] = ... }<\/code> to modify slice elements.<\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/04-go-variables-and-types\/\">Lesson 4: Go Variables and Types \u2013 Storing Data in Go<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/06-go-functions\/\">Lesson 6: Go Functions \u2013 Declarations, Returns, and defer<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Go or backend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led programming courses with hands-on projects.<\/p>\n<\/div>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"Does Go have a while loop?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Go has only for loops. Use for condition { } as a while-style loop.\"}},{\"@type\":\"Question\",\"name\":\"When should I use switch in Go?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use switch for multi-branch decisions. Go switch cases do not fall through by default.\"}},{\"@type\":\"Question\",\"name\":\"Can I use if with a short statement in Go?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes. if err := fn(); err != nil { } is a common Go pattern.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-go-programming"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"kindsonthegenius\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Go Programming Tutorial - Beginner to Expert Go Programming Tutorial\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial\" \/>\n\t\t<meta property=\"og:description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-07-07T20:29:05+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-07-07T20:38:07+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#blogposting\",\"name\":\"Go Control Flow \\u2013 if, else, switch, and for Loops - Go Programming Tutorial\",\"headline\":\"Go Control Flow \\u2013 if, else, switch, and for Loops\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\"},\"datePublished\":\"2026-07-07T20:29:05+00:00\",\"dateModified\":\"2026-07-07T20:38:07+00:00\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#webpage\"},\"articleSection\":\"Go Programming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"name\":\"Go Programming\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"position\":2,\"name\":\"Go Programming\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#listItem\",\"name\":\"Go Control Flow \\u2013 if, else, switch, and for Loops\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#listItem\",\"position\":3,\"name\":\"Go Control Flow \\u2013 if, else, switch, and for Loops\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/category\\\/go-programming\\\/#listItem\",\"name\":\"Go Programming\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"kindsonthegenius\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#webpage\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\",\"name\":\"Go Control Flow \\u2013 if, else, switch, and for Loops - Go Programming Tutorial\",\"description\":\"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \\u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/#author\"},\"datePublished\":\"2026-07-07T20:29:05+00:00\",\"dateModified\":\"2026-07-07T20:38:07+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial","description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in","canonical_url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#blogposting","name":"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial","headline":"Go Control Flow \u2013 if, else, switch, and for Loops","author":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"publisher":{"@id":"https:\/\/kindsonthegenius.com\/go\/#organization"},"datePublished":"2026-07-07T20:29:05+00:00","dateModified":"2026-07-07T20:38:07+00:00","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#webpage"},"isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#webpage"},"articleSection":"Go Programming"},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go#listItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/go","nextItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","name":"Go Programming"}},{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","position":2,"name":"Go Programming","item":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/","nextItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#listItem","name":"Go Control Flow \u2013 if, else, switch, and for Loops"},"previousItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#listItem","position":3,"name":"Go Control Flow \u2013 if, else, switch, and for Loops","previousItem":{"@type":"ListItem","@id":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/#listItem","name":"Go Programming"}}]},{"@type":"Organization","@id":"https:\/\/kindsonthegenius.com\/go\/#organization","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","url":"https:\/\/kindsonthegenius.com\/go\/"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author","url":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/","name":"kindsonthegenius","image":{"@type":"ImageObject","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","width":96,"height":96,"caption":"kindsonthegenius"}},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#webpage","url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","name":"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial","description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/#website"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#breadcrumblist"},"author":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"creator":{"@id":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/#author"},"datePublished":"2026-07-07T20:29:05+00:00","dateModified":"2026-07-07T20:38:07+00:00"},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/go\/#website","url":"https:\/\/kindsonthegenius.com\/go\/","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","inLanguage":"en-US","publisher":{"@id":"https:\/\/kindsonthegenius.com\/go\/#organization"}}]},"og:locale":"en_US","og:site_name":"Go Programming Tutorial - Beginner to Expert Go Programming Tutorial","og:type":"article","og:title":"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial","og:description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in","og:url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","article:published_time":"2026-07-07T20:29:05+00:00","article:modified_time":"2026-07-07T20:38:07+00:00","twitter:card":"summary_large_image","twitter:title":"Go Control Flow \u2013 if, else, switch, and for Loops - Go Programming Tutorial","twitter:description":"Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices. In this lesson we cover Go control flow \u2014 how programs make decisions and repeat work. You will learn go if else for loop patterns that appear in every real Go codebase: branching with if and switch, and iterating with for in"},"aioseo_meta_data":[],"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/kindsonthegenius.com\/go\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/\" title=\"Go Programming\">Go Programming<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGo Control Flow \u2013 if, else, switch, and for Loops\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/kindsonthegenius.com\/go"},{"label":"Go Programming","link":"https:\/\/kindsonthegenius.com\/go\/category\/go-programming\/"},{"label":"Go Control Flow \u2013 if, else, switch, and for Loops","link":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/"}],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial<\/title>\n<meta name=\"description\" content=\"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial\" \/>\n<meta property=\"og:description\" content=\"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/\" \/>\n<meta property=\"og:site_name\" content=\"Go Programming Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-07T20:29:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-07T20:38:07+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\"},\"headline\":\"Go Control Flow \u2013 if, else, switch, and for Loops\",\"datePublished\":\"2026-07-07T20:29:05+00:00\",\"dateModified\":\"2026-07-07T20:38:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\"},\"wordCount\":764,\"commentCount\":1,\"articleSection\":[\"Go Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\",\"name\":\"Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\"},\"datePublished\":\"2026-07-07T20:29:05+00:00\",\"dateModified\":\"2026-07-07T20:38:07+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\"},\"description\":\"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/2026\\\/07\\\/07\\\/05-go-control-flow\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Control Flow \u2013 if, else, switch, and for Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/\",\"name\":\"Go Programming Tutorial\",\"description\":\"Beginner to Expert Go Programming Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/#\\\/schema\\\/person\\\/1b5971e6d80495780d9857f155fd1b21\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/go\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial","description":"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","og_locale":"en_US","og_type":"article","og_title":"Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial","og_description":"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.","og_url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","og_site_name":"Go Programming Tutorial","article_published_time":"2026-07-07T20:29:05+00:00","article_modified_time":"2026-07-07T20:38:07+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21"},"headline":"Go Control Flow \u2013 if, else, switch, and for Loops","datePublished":"2026-07-07T20:29:05+00:00","dateModified":"2026-07-07T20:38:07+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/"},"wordCount":764,"commentCount":1,"articleSection":["Go Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","url":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/","name":"Go Control Flow \u2013 if, else, switch, and for Loops | Go Programming Tutorial","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/go\/#website"},"datePublished":"2026-07-07T20:29:05+00:00","dateModified":"2026-07-07T20:38:07+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21"},"description":"Master Go if else, switch, and for loops with runnable examples. Lesson 5 in the free Go programming tutorial series for beginners.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/go\/2026\/07\/07\/05-go-control-flow\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/go\/"},{"@type":"ListItem","position":2,"name":"Go Control Flow \u2013 if, else, switch, and for Loops"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/go\/#website","url":"https:\/\/kindsonthegenius.com\/go\/","name":"Go Programming Tutorial","description":"Beginner to Expert Go Programming Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/go\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/go\/#\/schema\/person\/1b5971e6d80495780d9857f155fd1b21","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/go\/author\/kindsonthegenius-3\/"}]}},"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/posts\/162\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/go\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}