c - Best way to switch do..while loops in a function? -
this c language only, still learning...
say have sort of function code here containing 2 do..while
loops:
int loops() { //execute loop first never again (instead, use second loop) { /*do something*/ } while(condition); { /*do something*/ } while(condition); return; }
what's best method run first do..while
loop @ first run, , when come function, run second do..while
loop?
you can use function parametre:
int loops (int loopnumber) {...}
or have 2 different functions.
if want use same function, way i'ld declare static variable check if function has been called previously:
int loops() { static int var = 0; int returnval = 0; if (!var) { //execute loop first never again (instead, use second loop) { /*do something*/ } while(condition); var = 1; } else { { /*do something*/ } while(condition); } return returnval; }
you can global variable, , code changes little:
int var = 0; int loops() { int returnval = 0; if (!var) { //execute loop first never again (instead, use second loop) { /*do something*/ } while(condition); var = 1; } else { { /*do something*/ } while(condition); } return returnval; }
Comments
Post a Comment