C Programming goto Statement

C Programming goto Statement

Go to statement in c programming; Through this tutorial, you will learn how to use go to statement in c program.

C Programming goto Statement

When a goto statement is encountered in a C program, the control jumps directly to the label mentioned in the goto statement

Syntax of goto Statement in c programming

See the following syntax of go to statement in c; as shown below:

goto label;
... .. ...
... .. ...
label: 
statement;

The label is an identifier. When the goto statement is encountered, the control of the program jumps to label: and starts executing the code.

Example 1 – Goto Statement in C program

See the following example for goto statement in c; as shown below:

#include <stdio.h>
int main()
{
   int sum=0;
   for(int i = 0; i<=10; i++){
	sum = sum+i;
	if(i==5){
	   goto addition;
	}
   }

   addition:
   printf("%d", sum);

   return 0;
}

Output

15

Explanation of above c program:

  • Iterate for loop and specify condition inside the loop is i == 5
  • Then jumping to this label using goto
  • This is reason the sum is displaying the sum of numbers till 5 even though the loop is set to run from 0 to 10

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *