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.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check Programming Books
- Apply for Computer Science Internship
- Check C++ Books
- Practice Computer Science MCQs
- Practice Programming MCQs