> C++ is already capable of jumping back on itself from previous parts of code
> though through the use of loops so it's not that different really
+1
>> Try to write two intersecting loops. Luckily it is impossible, but with goto it is quite possible
What is required for a programming language to be Turing complete is
a. support for conditional branching
b. the ability to modify an arbitrary number of memory locations (maintain an arbitrary number of variables)
The specific construct
goto is not a fundamental requirement of Turing-completeness; anything with equivalent expressive power would do. If we have a programming language with variables and assignment, and bounded or unbounded loops, it can do everything that a language with
if-else and
goto can do.
1 2 3 4
|
for( int i = 0 ; (i < 1) && !condition ; ++i )
{
// do something
}
|
is just a another way of writing
1 2 3 4 5
|
if( condition ) goto next ;
{
// do something
}
next:
|
If we actually need to write two intersecting loops in C++, doing it without the use of
goto is possible; though the misguided and misinformed zeal to avoid 'evil' may result in spaghetti-like code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
int main()
{
int i = 6 ;
int j = 6 ;
char jump = 'n' ;
for( ; i < 10 ; ++i )
{
if( i > 5 )
{
std::cout << "loop one begin \n" ;
std::cout << "first part of loop one code\n" ;
std::cout << "jump to loop two? " ;
if( std::cin >> jump )
{
std::cout << "answer: '" << jump << "'\n" ;
if( jump == 'y' || jump == 'Y' ) /* goto loop two */ { i = 0 ; j = 6 ; continue ; }
}
else return 0 ;
std::cout << "second part of loop one code\n" ;
std::cout << "loop one end \n" ;
}
else
{
for( ; j < 10 ; ++j )
{
if( j > 5 )
{
std::cout << "loop two begin \n" ;
std::cout << "first part of loop two code\n" ;
std::cout << "jump to loop one? " ;
if( std::cin >> jump )
{
std::cout << "answer: '" << jump << "'\n" ;
if( jump == 'y' || jump == 'Y' ) /* goto loop one */ { j = 20 ; i = 6 ; continue ; }
}
else return 0 ;
std::cout << "second part of loop two code\n" ;
std::cout << "loop two end \n" ;
}
}
}
}
}
|
http://coliru.stacked-crooked.com/a/93c0a99f2f1d110a