Here is an illustration of how much using loops can improve the readability of code. I'll use loops and gotos to solve a simple problem, creating a multiplication table:
Using for loops:
Code:
for (int a = 1; a <= 6; ++a) {
for (int b = a; b <= a * 6; b += a) {
std::cout << std::setw (3) << b << " ";
}
std::cout << "\n\n";
}
Directly converting those for loops to gotos:
Code:
int a = 1;
start_loop_a:;
if ( !(a <= 6)) goto end_loop_a;
int b = a;
start_loop_b:;
if ( !(b <= a * 6)) goto end_loop_b;
std::cout << std::setw (3) << b << " ";
b += a;
goto start_loop_b;
end_loop_b:;
std::cout << "\n\n";
++a;
goto start_loop_a;
end_loop_a:;
It is possible to do it using only one loop by adjusting the logic slightly, though not much more elegantly:
Code:
int a = 1;
int b = a;
start_loop_c:;
if ( !(a <= 6))
goto end_loop_c;
std::cout << std::setw (3) << b << " ";
b += a;
if ( !(b <= a * 6)) {
++a;
b = a;
std::cout << "\n\n";
}
goto start_loop_c;
end_loop_c:;