{"id":7226,"date":"2025-11-08T00:01:36","date_gmt":"2025-11-08T08:01:36","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7226"},"modified":"2025-11-01T13:10:53","modified_gmt":"2025-11-01T20:10:53","slug":"having-fun-with-goto-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7226","title":{"rendered":"Having Fun with <em>goto<\/em> &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7217\">C programming Exercise<\/a> is probably the most bizarre one I&#8217;ve ever offered! Using the <em>goto<\/em> keyword is frowned upon, which means that your C programmer brain is unaccustomed to thinking about using <em>goto<\/em> to construct, well, anything!<br \/>\n<!--more--><br \/>\nFor me, it&#8217;s been a while since I coded in BASIC, which uses a <code>GOTO<\/code> command to hop around the code. More relevant, however, is Assembly language: Assembly uses &#8220;jump&#8221; operands as branching instructions. In fact, the <em>goto<\/em> format in C heavily resembles the jump operand format used in Assembly. So this type of challenge may have been a bit easier for me to code, though it&#8217;s still weird.<\/p>\n<p>Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_11-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 = 0;\r\n    char b = 'A';\r\n\r\noutput:\r\n    if( a==10 ) goto end;\r\n    printf(\"%c%d  \",b,a);\r\n    b++;\r\n    if( b=='K' )\r\n    {\r\n        putchar('\\n');\r\n        a++;\r\n        b='A';\r\n    }\r\n    goto output;\r\n\r\nend:\r\n    return 0;\r\n}<\/pre>\n<p>This code doesn&#8217;t even look like C &mdash; but it is!<\/p>\n<p>The first label is <code>output<\/code>, which is kinda the start of the outer nested loop. It&#8217;s followed by an <em>if<\/em> statement to test the value of variable <code>a<\/code>. When <code>a<\/code> is equal to 10, execution branches to the <code>end<\/code> label, the program is done. Otherwise, the program outputs the values of variables <code>b<\/code> and <code>a<\/code>, which have been initialized to &#8216;A&#8217; and zero, respectively.<\/p>\n<p>Variable <code>b<\/code> tracks the inner loop. An <em>if<\/em> test determines when <code>b<\/code> is equal to &#8216;K&#8217;. If true, the newline is output, variable <code>a<\/code> is incremented (for the next row of output), and variable <code>b<\/code> is reset back to the value &#8216;A&#8217;.<\/p>\n<p>When variable <code>b<\/code> is less than &#8216;K&#8217;, a <em>goto<\/em> statement jumps execution back to the <code>output<\/code> label, where the process repeats.<\/p>\n<p>Here is sample output, which is identical to the (more proper) nested loop version of the code:<\/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>I hope that you can see how <em>goto<\/em> made a nested loop not look like a nested loop. The code above still has a poetic beauty to it, but its purpose is obfuscated. Even an experienced programmer must step through the code line by line to determine what exactly is going on.<\/p>\n<p>Remember that your solution need not look exactly like mine. In fact, if you have a different solution, consider pasting it in the comments. Use the <code>&lt;pre&gt;<\/code> tags to ensure that the formatting doesn&#8217;t go all bonkers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s C programming Exercise is probably the most bizarre one I&#8217;ve ever offered! Using the goto keyword is frowned upon, which means that your C programmer brain is unaccustomed to thinking about using goto to construct, well, anything!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7226","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7226","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=7226"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7226\/revisions"}],"predecessor-version":[{"id":7243,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7226\/revisions\/7243"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}