是否可以从循环外部结束放置在循环内的函数?
Is it possible to end a function that is placed within a loop from outside the loop?
在 do...while
循环中调用一次函数后,如何结束函数?例如,在下面的代码中,我希望 myfunc()
只被调用一次,然后在循环中不再被调用。这可能吗?
do{
myfunc();
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, fb_width, fb_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
drawSomething();
glfwSwapBuffers(w);
glfwPollEvents();
}
while (something);
你可以把它放在 do while 循环之前
或者你可以持有一个标志变量来存储函数是否已经执行过。
int flag = 0;
do {
if(flag==0){
myfunc();
flag=1;
}
... // Rest of the loop
}
通常可以使用统计第一次通过的静态变量来完成。实现可能取决于它是在调用中还是在回调中。
static int first = 0;
do{
if(!first){
myfunc();
first = 1;
}
glClear(GL_COLOR_BUFFER_BIT);
// the rest of your code
}while something;
first = 0; //or not zero it if you want to remember the state
在 do...while
循环中调用一次函数后,如何结束函数?例如,在下面的代码中,我希望 myfunc()
只被调用一次,然后在循环中不再被调用。这可能吗?
do{
myfunc();
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, fb_width, fb_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
drawSomething();
glfwSwapBuffers(w);
glfwPollEvents();
}
while (something);
你可以把它放在 do while 循环之前
或者你可以持有一个标志变量来存储函数是否已经执行过。
int flag = 0;
do {
if(flag==0){
myfunc();
flag=1;
}
... // Rest of the loop
}
通常可以使用统计第一次通过的静态变量来完成。实现可能取决于它是在调用中还是在回调中。
static int first = 0;
do{
if(!first){
myfunc();
first = 1;
}
glClear(GL_COLOR_BUFFER_BIT);
// the rest of your code
}while something;
first = 0; //or not zero it if you want to remember the state