The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C goto statement

goto statement

goto statement in c sometimes called as goto operation in other languages like php.

Why goto Statement

C programming introduces a goto statement by which you can jump directly to a line of code within the same file.

Syntax And Facts

  • The goto statement does not require any condition.
  • The goto statement simply passes control anywhere in a program without testing any condition.

C goto Syntax

Syntax goto Statement

Syntax
goto Label;
.
.
Label:
{
statement n;
}

goto statement Flowchart

The following flowchart will clearly demonstrate, how goto statement works

goto statement flowchart

goto statement Program

Let's take a look at the goto statement in action. In the following program we will print whether the number is positive or negative using goto statement

goto.c
#include <stdio.h>
int main()
{
int num;
printf("Enter a positive or negative integer number:\n ");
scanf("%d",&num);
if(num >= 0)
goto pos;
else
goto neg;
pos:
{
printf("%d is a positive number",num);
return 0;
}
neg:
{
printf("%d is a negative number",num);
return 0;
}
}
  • Enter a positive or negative integer number:
  • 2
  • 2 is a positive number

Note:

In this program, the user entered number is checked for positive or negative number. If the user entered number is positive, the control is passes to pos : by goto statement. If the user entered number is negative, the control is passes to neg : by goto statement.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report