gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > breaking a "for..." loop

#75985 - deltree - Fri Mar 17, 2006 12:40 pm

hi,
I made this code:

Code:
 for (i=0;i<4;i++) //colonne
     {
          for (j=0;j<4;j++) //ligne
         {
            if    ((block[current_shape].shape[i][j+block[current_shape].pos]=='X') && (aire[j+yoffset+1][i+xoffset]!='.'))
            {
                insert();
                ham_VBAText("collision under\n");
                return 1;
            }
            if    ((block[current_shape].shape[i][j+block[current_shape].pos]=='X') && (aire[j+yoffset][i+xoffset+1]!='.'))
            {
            ham_VBAText("collision right\n");
                return 2;
            }
            if    ((block[current_shape].shape[i][j+block[current_shape].pos]=='X') && (aire[j+yoffset][i+xoffset-1]!='.'))
            {
            ham_VBAText("collision left\n");
                return 3;
            }
        }
     }

but I wonder about something:
does the "return" immediatly break the "for" loop and exit the function ?
or is the "for" loop prioritary and will only return the last "return" value at the end ?

#75992 - sgeos - Fri Mar 17, 2006 1:49 pm

return ends the function.

If you want to return at the end, do this:
Code:
int rval;

for (...)
{
  if (...)
    rval = 1;
  if (...)
    rval = 2;
}
return rval;

-Brendan

#76018 - deltree - Fri Mar 17, 2006 7:30 pm

thanx.