Go to statement in C

Go to statement in C

Hello! In today's blog, we will study go to statement and I will try that as this blog end you will able to understand go to statement very comfortably. In the previous blog, we learned the concept of if…else and switch statements. I hope you liked my previous blog.


GOTO Statement in C Language


What is the goto statement?

So far we have discussed ways of controlling the flow of execution based on certain specified conditions. Like any other language, C supports the goto statements to branch unconditionally from one point to another in the program. although it may not be essential to use a goto statement In a highly structured language like C, there may be occasions when to use goto might be desirable.

The goto statement is known as the jump statement in C. As the name suggests, the goto is used to transfer the program control to a predefined label. The goto statement can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement. However, using goto is avoided these days since it makes the program less readable and complicated.
The go to statement is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.

Syntax :-

There are two types of jump in goto statements:
 
1. Forward jump
2. Backward jump

 

Syntax1      |   Syntax2

--------------------------

goto label;  |    label: 

.                    |    .

label:           |    goto label;

 

 Type 1: In this case, we will see a situation similar to as shown in Syntax1 above. Suppose we need to write a program where we need to check if a number is even or not and print accordingly using the goto statement. Below program explains how to do this:

Example:-

#include <stdio.h> 

// function to check even or not

void checkEvenOrNot(int num)

{

    if (num % 2 == 0)

        // jump to even

        goto even; 

    else

        // jump to odd

        goto odd; 

  even:

    printf("%d is even", num);

    // return if even

    return; 

odd:

    printf("%d is odd", num);

} 

int main() {

    int num = 26;

    checkEvenOrNot(num);

    return 0;

}

Type 2:- In this case, we will see a situation similar to as shown in Syntax1 above.
Suppose we need to write a program that prints numbers from 1 to 10 using the goto statement.
The program explains how to do this.

Example:-

// function to print numbers from 1 to 10

void printNumbers()

{

    int n = 1;

label:

    printf("%d ",n);

    n++;

    if (n <= 10)

        goto label;

}

  //driver code

int main() {

    printNumbers();

    return 0;

}

 

 

Advantage:-


1.using goto statement you can alter the normal sequence of the program execution so it gives the power to jump to any part of the program.

 Drawbacks of using goto statement

There are some drawbacks of goto also, the main drawback of goto are as follows.

1.The use of goto statement makes the program logic very complex.

2.use of goto makes the task of analyzing and verifying the correctness of programs very difficult.

3.Use of goto can be simply avoided using break and continue statements.

-------


 


Comments

Post a Comment

Popular posts from this blog

Decision making with if statement and else..if

Switch statement in C language

Call by value and by reference in function