As an example:
function()
{
calculation1();
while(1)
{
if (test1() == FAIL) { break; }
calculation2();
if (test2() == FAIL) { break; }
calculation3();
if (test3() == FAIL) { break; }
calculation4();
break;
}
final_calculation();
return;
}
Each test depends on results obtained from all calculations before it. If a test fails, however, the rest of the calculations should be skipped.
An alternative approach would be to use a series of nested if() statements:
function()
{
calculation1();
if (test1() == SUCCESS)
{
calculation2();
if (test2() == SUCCESS)
{
calculation3();
if (test3() == SUCCESS)
{
calculation4();
}
}
}
final_calculation();
return;
}
However this latter approach starts looking horribly messy in anything but a highly abstract example like above. I believe the former approach scales much better to longer, more complicated code. Are there any reasons not to go with the first method?