{"id":2318,"date":"2024-05-10T07:20:31","date_gmt":"2024-05-10T07:20:31","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2318"},"modified":"2024-05-10T07:20:31","modified_gmt":"2024-05-10T07:20:31","slug":"goto-statement-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/goto-statement-in-c\/","title":{"rendered":"Goto Statement In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#goto-statement-in-c\">Goto Statement In C<\/a><\/li><li ><a href=\"#example-type-1\">Example: Type 1 <\/a><\/li><li ><a href=\"#example-type-2\">Example: Type 2 <\/a><\/li><li ><a href=\"#disadvantages-of-using-goto-statement\">Disadvantages of Using Goto Statement<\/a><\/li><li ><a href=\"#faq-goto-statement-in-c\">FAQ- Goto Statement In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"goto-statement-in-c\">Goto Statement In C<\/h2>\n\n\n\n<p>The <code>goto<\/code> statement in the C programming language. The <code>goto<\/code> statement is indeed a jump statement that allows for unconditional transfers of control within a function. It allows you to transfer the program&#8217;s control to a specific labeled location within the same function or code block.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p>Syntax1 | Syntax2<\/p>\n\n\n\n<p> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n\n\n\n<p> goto label; | label:<\/p>\n\n\n\n<p> .              |          .<\/p>\n\n\n\n<p> .              |          .<\/p>\n\n\n\n<p> .              |          .<\/p>\n\n\n\n<p> label:      |           goto label;<\/p>\n\n\n\n<p>The <code>goto<\/code> statement is used to transfer control to a labeled statement within the same function or code block. The label is indeed a user-defined identifier, and the statement immediately following the label is the destination statement.<\/p>\n\n\n\n<p>You define a label using an identifier followed by a colon (<code>:<\/code>). For example:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>You place this label before the statement you want to mark as the destination.<\/li>\n\n\n\n<li>To jump to the labeled statement, you use the <code>goto<\/code> statement followed by the label&#8217;s identifier. For example:<\/li>\n\n\n\n<li>The control flow will transfer to the statement marked by the label when the <code>goto<\/code> statement is executed.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-type-1\">Example: Type 1 <\/h2>\n\n\n\n<p>Type 1: Here&#8217;s an example of a C program that uses the <code>goto<\/code> statement to check if a number is even or not and then prints the result accordingly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to check if a number is \n\/\/ even or not using goto statement \n#include &lt;stdio.h&gt; \n  \n\/\/ function to check even or not \nvoid checkEvenOrNot(int num) \n{ \n    if (num % 2 == 0) \n        \/\/ jump to even \n        goto even; \n    else\n        \/\/ jump to odd \n        goto odd; \n  \neven: \n    printf(\"%d is even\", num); \n    \/\/ return if even \n    return; \nodd: \n    printf(\"%d is odd\", num); \n} \n  \nint main() \n{ \n    int num = 26; \n    checkEvenOrNot(num); \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>26 is even\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-type-2\">Example: Type 2 <\/h2>\n\n\n\n<p>Type 2: Here&#8217;s an example of a C program that uses the <code>goto<\/code> statement to print numbers from 1 to 10<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to print numbers \n\/\/ from 1 to 10 using goto statement \n#include &lt;stdio.h&gt; \n  \n\/\/ function to print numbers from 1 to 10 \nvoid printNumbers() \n{ \n    int n = 1; \nlabel: \n    printf(\"%d \", n); \n    n++; \n    if (n &lt;= 10) \n        goto label; \n} \n  \n\/\/ Driver program to test above function \nint main() \n{ \n    printNumbers(); \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"disadvantages-of-using-goto-statement\">Disadvantages of Using Goto Statement<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Complexity<\/strong>: The <code>goto<\/code> statement can make program logic complex and hard to follow. It can create &#8220;spaghetti code&#8221; where the flow of control is not evident, making it difficult for programmers to understand and maintain the code.<\/li>\n\n\n\n<li><strong>Difficulty in Tracing<\/strong>: Using <code>goto<\/code> can make it challenging to trace the flow of the program, which is crucial for debugging and understanding how the code works.<\/li>\n\n\n\n<li><strong>Verification Difficulty<\/strong>: Programs that use, especially in loops, can be challenging to analyze and verify for correctness. It becomes harder to reason about the program&#8217;s behavior and ensure it behaves as intended.<\/li>\n\n\n\n<li><strong>Alternatives<\/strong>: In many cases, structured control flow constructs like loops (<code>for<\/code>, <code>while<\/code>, <code>do-while<\/code>) and conditional statements (<code>if<\/code>, <code>else<\/code>) provide more readable and maintainable alternatives to achieve the same tasks. Additionally, <code>break<\/code> and <code>continue<\/code> statements are often used to control loops without resorting to <code>goto<\/code><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-goto-statement-in-c\">FAQ- Goto Statement In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696489221536\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a goto statement in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. the <code>goto<\/code> statement in C. It is indeed a jump statement that allows you to transfer control from one part of the code to any other part of the code within the same function or code block. The <code>goto<\/code> statement provides the flexibility to alter the normal flow of a program according to specific needs or conditions<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696489227183\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is a goto statement in C with an example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The use of a goto statement may lead to code that is buggy and hard to follow. For example, one:\u00a0<strong>for (i = 0; i &lt; number; ++i) { test += i; goto two; } two: if (test > 5) { goto three; }<\/strong>\u00a0&#8230; .. &#8230; Also, the goto statement allows you to do bad stuff such as jump out of the scope<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696489235133\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Is goto a jump statement in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. C program consists of three types of Jump Statements in C, namely, break, continue, and goto.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Goto Statement In C The goto statement in the C programming language. The goto statement is indeed a jump statement that allows for unconditional transfers of control within a function. It allows you to transfer the program&#8217;s control to a specific labeled location within the same function or code block. Syntax Syntax1 | Syntax2 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8230; <a title=\"Goto Statement In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/goto-statement-in-c\/\" aria-label=\"More on Goto Statement In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5347,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[224],"tags":[418],"class_list":["post-2318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-question-answer","tag-goto-statement-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":10649,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2318\/revisions\/10649"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5347"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}