c语言替代goto语句的办法 - afa2010 - 博客园 (cnblogs.com)
tryagain:
int res = doSomething();
//......
if (res < 0) {
goto tryagain;
}
else{
doSomething2();
}
替代方法,用do{} while(0)替代:
do{
int res = doSomething();
//......
if (res < 0) {
continue;
}
else{
doSomething2();
}
}while(0);
还有这种goto:
int res = doSomething();
//......
if (res < 0) {
goto errorOccur;
}
else{
doSomething2();
}
errorOccur:
doThing();
换成
do{
int res = doSomething();
//......
if (res < 0) {
break;
}
else{
doSomething2();
}
}while(0);
doThing();