C tutorial - goto statement in C programming language

Programming languages or concepts
0

 goto statement in C programming language 

In this C tutorial, We learn how goto statement use in the C program / code ,The goto statement is known as jump statement in C programming, goto statement define using goto keyword, The goto statment can be used to repeat some part of the code for a particular condition. We can using more than one goto statement in single program with different labels.

Def.

The goto statement can be used to jump from anywhere to anywhere within a function.

 goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function


Syntax

The syntax for a goto statement in C is as follows −


goto label;

............

............

label: statement;



Following is a simple program for goto statement


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 = 46; 


    checkEvenOrNot(num); 


    return 0; 


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

Post a Comment

0Comments

Post a Comment (0)
close