{"id":7217,"date":"2025-11-01T00:01:24","date_gmt":"2025-11-01T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7217"},"modified":"2025-11-08T10:00:26","modified_gmt":"2025-11-08T18:00:26","slug":"having-fun-with-goto","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7217","title":{"rendered":"Having Fun with <em>goto<\/em>"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>Among the C language&#8217;s slender assortment of keywords you find <em>goto<\/em>. It&#8217;s a branching statement, which can be used to loop or to escape from some ugly programming knot. Regardless, you&#8217;re told early on in your C language education that you must avoid this keyword at all costs! That is, unless you want to try this month&#8217;s exercise.<br \/>\n<!--more--><br \/>\nAs a review, the <em>goto<\/em> keyword branches program execution to a label. Labels are things you probably don&#8217;t use often in C programming, though they do appear inside a <em>switch-case<\/em> structure. The label is the only thing on a line of text. It&#8217;s an identifier (named like a variable) immediately followed by a colon:<\/p>\n<p><code>there:<\/code><\/p>\n<p>This label serves as a target for the <em>goto<\/em> statement:<\/p>\n<p><code>goto there;<\/code><\/p>\n<p>The <em>goto<\/em> statement ends with a semicolon, which is something you might forget (or use a colon instead). After encountering the above statement, program flow jumps to the <code>there<\/code> label and continues execution. It&#8217;s this aspect of <em>goto<\/em> that requires the warning: Using <em>goto<\/em> to hippity-hop around your source code makes the code difficult to read and lends itself to other unnecessary problems. The descriptive term is &#8220;spaghetti code,&#8221; which isn&#8217;t flattering. Even so, <em>goto<\/em> is a valid keyword and has never been deprecated.<\/p>\n<p>Consider the following code, which demonstrates a nested loop:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_11_01-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n    char b;\r\n\r\n    for( a=0; a&lt;10; a++ )\r\n    {\r\n        for( b='A'; b&lt;'K'; b++ )\r\n        {\r\n            printf(\"%c%d  \",b,a);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Like most nested loops I can think of, this one processes a matrix, rows of numbers and letters, all neatly presented in a grid:<\/p>\n<p><code>A0&nbsp;&nbsp;B0&nbsp;&nbsp;C0&nbsp;&nbsp;D0&nbsp;&nbsp;E0&nbsp;&nbsp;F0&nbsp;&nbsp;G0&nbsp;&nbsp;H0&nbsp;&nbsp;I0&nbsp;&nbsp;J0&nbsp;&nbsp;<br \/>\nA1&nbsp;&nbsp;B1&nbsp;&nbsp;C1&nbsp;&nbsp;D1&nbsp;&nbsp;E1&nbsp;&nbsp;F1&nbsp;&nbsp;G1&nbsp;&nbsp;H1&nbsp;&nbsp;I1&nbsp;&nbsp;J1&nbsp;&nbsp;<br \/>\nA2&nbsp;&nbsp;B2&nbsp;&nbsp;C2&nbsp;&nbsp;D2&nbsp;&nbsp;E2&nbsp;&nbsp;F2&nbsp;&nbsp;G2&nbsp;&nbsp;H2&nbsp;&nbsp;I2&nbsp;&nbsp;J2&nbsp;&nbsp;<br \/>\nA3&nbsp;&nbsp;B3&nbsp;&nbsp;C3&nbsp;&nbsp;D3&nbsp;&nbsp;E3&nbsp;&nbsp;F3&nbsp;&nbsp;G3&nbsp;&nbsp;H3&nbsp;&nbsp;I3&nbsp;&nbsp;J3&nbsp;&nbsp;<br \/>\nA4&nbsp;&nbsp;B4&nbsp;&nbsp;C4&nbsp;&nbsp;D4&nbsp;&nbsp;E4&nbsp;&nbsp;F4&nbsp;&nbsp;G4&nbsp;&nbsp;H4&nbsp;&nbsp;I4&nbsp;&nbsp;J4&nbsp;&nbsp;<br \/>\nA5&nbsp;&nbsp;B5&nbsp;&nbsp;C5&nbsp;&nbsp;D5&nbsp;&nbsp;E5&nbsp;&nbsp;F5&nbsp;&nbsp;G5&nbsp;&nbsp;H5&nbsp;&nbsp;I5&nbsp;&nbsp;J5&nbsp;&nbsp;<br \/>\nA6&nbsp;&nbsp;B6&nbsp;&nbsp;C6&nbsp;&nbsp;D6&nbsp;&nbsp;E6&nbsp;&nbsp;F6&nbsp;&nbsp;G6&nbsp;&nbsp;H6&nbsp;&nbsp;I6&nbsp;&nbsp;J6&nbsp;&nbsp;<br \/>\nA7&nbsp;&nbsp;B7&nbsp;&nbsp;C7&nbsp;&nbsp;D7&nbsp;&nbsp;E7&nbsp;&nbsp;F7&nbsp;&nbsp;G7&nbsp;&nbsp;H7&nbsp;&nbsp;I7&nbsp;&nbsp;J7&nbsp;&nbsp;<br \/>\nA8&nbsp;&nbsp;B8&nbsp;&nbsp;C8&nbsp;&nbsp;D8&nbsp;&nbsp;E8&nbsp;&nbsp;F8&nbsp;&nbsp;G8&nbsp;&nbsp;H8&nbsp;&nbsp;I8&nbsp;&nbsp;J8&nbsp;&nbsp;<br \/>\nA9&nbsp;&nbsp;B9&nbsp;&nbsp;C9&nbsp;&nbsp;D9&nbsp;&nbsp;E9&nbsp;&nbsp;F9&nbsp;&nbsp;G9&nbsp;&nbsp;H9&nbsp;&nbsp;I9&nbsp;&nbsp;J9&nbsp;&nbsp;<\/code><\/p>\n<p>Your task for this month&#8217;s Exercise is to generate identical output by using <em>goto<\/em> statements and labels instead of nested <em>for<\/em> loops.<\/p>\n<p>The difficulty level for this challenge to three out of four stars, which isn&#8217;t because the program itself is tough to code. No, the problem is that your C programming brain isn&#8217;t trained to think of <em>goto<\/em> as a way to accomplish this task. Yes, even though the solution presents as C code, it&#8217;s not an approach any C programmer would ever consider!<\/p>\n<p>Have fun with this one! I know that I did. I marveled at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7226\">my solution<\/a>, not because it was anything that would win an award, but that it was entirely possible using a weirdo approach to C coding that I&#8217;ve never considered.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s the keyword you should avoid &mdash; except for in this challenge! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7217\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-7217","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7217"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7217\/revisions"}],"predecessor-version":[{"id":7246,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7217\/revisions\/7246"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}