Goto Statement in C++

Goto Statement in C++

Goto statement in C++ is an unconditional jump statement used for transferring the control of a program to a given label. It allows the program’s execution flow to jump to a certain location within the function.

 

Syntax of goto statement 

goto label;

//block of statements

label: statement;

Here label is an identifier that identifies a labeled statement and this statement is any statement that is preceded by an identifier followed by a colon (:)

Program of goto statement in C++

#include <iostream>  
using namespace std;  
int main()  
{  
ineligible:    
         cout<<"You are not eligible to vote\n";    
      cout<<"Enter your age:\n";    
      int age;  
      cin>>age;  
      if (age < 18){    
              goto ineligible;    
      }    
      else    
      {    
              cout<<"You are eligible";     
      }         
}  

Output

Enter your age:
21
You are eligible

 

Read more, Switch Statement in C++

Share this post

Leave a Reply

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