This C++ program illustrates goto statement. The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label. The program prints numbers 1 to 10 without using any loop.
Here is the source code of the C++ program which illustrates goto statement. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to illustrate goto statement
*/
#include <iostream>
using namespace std;
int main () {
int i = 0;
cout << "Printing 1 to 10 without using loop\n";
repeat:
cout << ++i << " ";
if(i != 10)
goto repeat;
cout << "\n";
}
$ gcc test.cpp $ a.out Printing 1 to 10 without using loop 1 2 3 4 5 6 7 8 9 10
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Buy Programming Books
- Buy C++ Books
- Practice Programming MCQs
- Buy Computer Science Books