- Home /
where to put return and break conditions
I know that break; is for ending a loop early, and return; is for finishing a function early and returning a value
I have an instance in the middle of a function where I am comparing 20 values and if any 2 of them meet the conditions I want to finish the comparisons and go to the next step.
Should I do a loop of only one length using break;? If I did a function it would have 20 variables which seems impractical. is there a different solution?
I think a code snippet might help explain exactly what you mean, unless SubatomicHero's answer sorts you. Since you said 20 variables its sounds like you don't have something so easy to compare?
Answer by SubatomicHero · Feb 26, 2013 at 04:26 PM
So I imagine your doing your comparisons in a for loop or foreach loop? If so you need to break out of that loop and whatever else is in the function can continue to run.
An example:
int x = 4;
for (int i = 0; i < 10; i++)
{
if (i == x)
{
Debug.log("I is equal to x with the value of: " + i);
break;
}
}
// do other code here
Your answer
![](https://koobas.hobune.stream/wayback/20220613093350im_/https://answers.unity.com/themes/thub/images/avi.jpg)