{"id":2615,"date":"2017-07-22T00:01:08","date_gmt":"2017-07-22T07:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2615"},"modified":"2017-07-29T07:13:08","modified_gmt":"2017-07-29T14:13:08","slug":"goto-hell","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2615","title":{"rendered":"Goto Hell"},"content":{"rendered":"<p>The only reason anyone learns about the C language keyword <em>goto<\/em> is because it&#8217;s one of the original 32 keywords. The next thing a programmer learns is to avoid it. Peril, pestilence, and pain awaits the fool who uses <em>goto<\/em>. Yea verily, if a programming Hell exists, <em>goto<\/em> is the command you use to get there.<br \/>\n<!--more--><br \/>\nStill, wizards Brian Kernighan and Dennis Ritchie sought to include <em>goto<\/em> in the C language. It could have been a holdover from the B language or, in their wisdom, they figured it had purpose that no other tool could address. Yet, I can&#8217;t think of any situation where an experienced programmer has tossed up his hands and relented to use <em>goto<\/em> in his code.<\/p>\n<p>In their original edition of <em>The C Programming Language<\/em>, Kernighan and Ritchie write:<\/p>\n<blockquote><p>C provides the infinitely-abusable <em>goto<\/em> statement, and labels to branch to. Formally, the <em>goto<\/em> is never necessary, and in practice it is almost always easy to write code without it.<\/p><\/blockquote>\n<p>I remember using <em>goto<\/em> in the BASIC programming language. It&#8217;s an unconditional branching statement that alters program flow. Most frequently, it&#8217;s used with an <em>if<\/em> statement because BASIC (at the time) lacked a way to group statements as the curly brackets do in C:<\/p>\n<p><code>IF X = 0 GOTO 40<\/code><\/p>\n<p>The problem with <em>goto<\/em>, and all of early BASIC, is that it disrupts program flow to the point that code is difficult to read and follow. Some programmers preferred things that way, specifically to obfuscate their work and prevent others from copying it. Such &#8220;spaghetti code&#8221; lead to the demise of BASIC, despite later efforts to improve the language.<\/p>\n<p>As an example of spaghetti code, consider the following:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    puts(\"This is Line 1\");\r\n    goto this;\r\nthat:\r\n    puts(\"This is Line 3\");\r\n    goto theother;\r\nbackhere:\r\n    puts(\"This is Line 5\");\r\n    goto end;\r\nthis:\r\n    puts(\"This is Line 2\");\r\n    goto that;\r\ntheother:\r\n    puts(\"This is Line 4\");\r\n    goto backhere;\r\nend:\r\n\r\n    return(0);\r\n}<\/pre>\n<p>What a mess!<\/p>\n<p>Each <em>goto<\/em> statement references a label. In C, a label is a word (named like a variable) followed by a colon. The label typically begins a line, as shown above where <em>goto<\/em> causes program to hippity-hoppity from place to place. The code could easily be re-written in a variety of ways, but my point was to show how <em>goto<\/em> can turn things ugly quick. Here&#8217;s the output:<\/p>\n<pre><code>This is Line 1\r\nThis is Line 2\r\nThis is Line 3\r\nThis is Line 4\r\nThis is Line 5<\/code><\/pre>\n<p>Internally, <em>goto<\/em> is a necessary part of computer programming. At the machine code level, a <em>goto<\/em> is known as a &#8220;jump.&#8221; It&#8217;s a way to load the processor&#8217;s instruction pointer (IP) with a specific memory address. Machine code features a lot of jumps, some conditional but many direct. Even Kernighan and Ritchie mention that <em>goto<\/em> could be necessary in extreme cases. I&#8217;ll explore this topic in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2622\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most despised keyword in the C language universe. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2615\">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":[2],"tags":[],"class_list":["post-2615","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2615","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=2615"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2615\/revisions"}],"predecessor-version":[{"id":2635,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2615\/revisions\/2635"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}