Один случай, когда можно использовать оператор GOTO...
??? ?? ???? ????????? ????????????? ??????? ? ????? ?????????? ?????????? ? ????????? goto. ???????? ???, ? ????, ? ????????? ???????? ??? ?????????????. ?? ????????? ????????? ? ????? ???????, ????? ????????????? ????????? goto ?? ???????? ????? ?? ????????????.
??? ?????? ??????????? ????, ??? ??? ????? ????????? ???? ???????, ? ?????? ????????? ???????-?????, ???? ??? ??? ??????? ?????????. ??? ??? ????? ????????? ??????????? ?????!
void Try1(void)
{
if (!Cond(1)) {
printf("Error with condition 1!");
} else if(!Cond(2)) {
printf("Error with condition 2!");
} else if(!Cond(3)) {
printf("Error with condition 3!");
} else if(!Cond(4)) {
printf("Error with condition 4!");
} else if(!Cond(5)) {
printf("Error with condition 5!");
} else {
// My real code
printf("Hurra!");
}
}
??????????? ???????????????? ?????! Goto ???? ???! ?? ??????, ????-???? ???????? ????????. ?????? ??? ????? ????????? ???? ????? ???? ???????, ?????? ?????? ??????? ???????? ?????? ??????? _?_ ?????? ????????? ?????? ????? ???? ?????? ?????? ???? ??? ?????????? ??????? ?????????. ? ????? ????? ?????. ??, ??, ?? ??? ? ??????? ????? ????... ????? ???????? ????????? ?????? ? ?????, ??? ???, ?????????...
??? ??? ????????? ????? ????????? ? ???????????? ??????????? ????????????????? ??, ???-?? ?????:
void
Try2(void)
{
Condition * c1 = new Condition();
if (!c1->Cond(1)) {
printf("Error with condition 1!");
} else {
Condition * c2 = new Condition();
if (!c2->Cond(2)) {
printf("Error with condition 2!");
} else {
Condition * c3 = new Condition();
if (!c3->Cond(3)) {
printf("Error with condition 3!");
} else {
Condition * c4 = new Condition();
if (!c4->Cond(4)) {
printf("Error with condition 4!");
} else {
Condition * c5 = new Condition();
if (!c5->Cond(5)) {
printf("Error with condition 5!");
} else {
printf("Hurra!");
}
delete c5;
}
delete c4;
}
delete c3;
}
delete c2;
}
delete c1;
return;
}
??????? ?????? ???????, ??????? ? ?????? ??????????? ???? 10-20 ????? ??????? ???? ?? ??????. ? ???? ? ???????? ???????????????? ??? ????????? ?????? ? ?????, ???????? ??? ????????????? COM ???????? ??? try-catch ? C# ??? ?????... ??? ??? ????????? ??? ??? ???????? ???, ????? ??? ???????? ??? ?? ??? ?????? ?? ???????? ??????, ????? ??????? ????? ?????? ????????? ???????????
??? ?? ???????, ??? ??? ?? ?????? ????? ?????? ?????? ???????? goto. ?? ??????? ???? ? ??????, ??? ?? ????. ?????????, ??? ??? ???????? ? ???:
void Try3(void)
{
Condition1 * c1 = new Condition1();
if (!c1->Cond()) {
printf("Error with condition 1!");
goto Exit;
}
Condition2 * c2 =
new Condition2();
if (!c2->Cond()) {
printf("Error with condition 2!");
goto Exit;
}
Condition3 * c3 =
new Condition3();
if (!c3->Cond()) {
printf("Error with condition 3!");
goto Exit;
}
Condition1 * c4 =
new Condition4();
if (!c4->Cond()) {
printf("Error with condition 1!");
goto Exit;
}
Condition1 * c5 =
new Condition5();
if (!c5->Cond()) {
printf("Error with condition 5!");
goto Exit;
}
// My real code
printf(
"Hurra!");
Exit:
if (NULL != c1 ) delete c1;
if (NULL != c2 ) delete c2;
if (NULL != c3 ) delete c3;
if (NULL != c4 ) delete c4;
if (NULL != c5 ) delete c5;
return;
}
? ??????, ????? ????????????? ??? ???????, ??????? ????? ????? ? ?????:
#define Check(cond) if (!(cond)) { printf("Error with condition "#cond); goto Exit; }
void
Try4(void)
{
Condition1 * c1 = new Condition1();
Check(c1->Cond());
Condition2 * c2 =
new Condition2();
Check(c2->Cond());
Condition3 * c3 =
new Condition3();
Check(c3->Cond());
Condition4 * c4 =
new Condition4();
Check(c4->Cond());
Condition5 * c5 =
new Condition5();
Check(c5->Cond());
// My real code
printf("Hurra!");
Exit:
if (NULL != c1 ) delete c1;
if (NULL != c2 ) delete c2;
if (NULL != c3 ) delete c3;
if (NULL != c4 ) delete c4;
if (NULL != c5 ) delete c5;
return;
}
???????, "?? ????? ? ???? ???????? ???", ?? ??? ???????, ??? ? ???? ?????? ???????? goto ???-???? ????? ????????????. ? ??? ?? ?? ???? ????????