Search Your Topic

config
Monday, 12 June 2017 01:47

C++ Goto Statement

Written by
Rate this item
(0 votes)

The C++ goto statement is also known as jump statement. It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label.

It can be used to transfer control from deeply nested loop or switch case label.


C++ Goto Statement Example

Let's see the simple example of goto statement in C++.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. ineligible:    
  6.          cout<<"You are not eligible to vote!\n";    
  7.       cout<<"Enter your age:\n";    
  8.       int age;  
  9.       cin>>age;  
  10.       if (age < 18){    
  11.               goto ineligible;    
  12.       }    
  13.       else    
  14.       {    
  15.               cout<<"You are eligible to vote!";     
  16.       }         
  17. }  

Output:

You are not eligible to vote!
Enter your age:
16
You are not eligible to vote!
Enter your age:
7
You are not eligible to vote!
Enter your age:
22
You are eligible to vote!

 

Read 165 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ Continue Statement Functions »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.