Break acting like return C#

I’m having a problem where the break; command is acting like return; and ending the whole script.
The function of the script it a 1D bin packing
At the end of the code, i have a for loop inside another for loop, but when the script encounters the break;, it seems to end the script.
as seen in the screenshot, the bottom component (scripted in python) runs thru all the numbers, but the one on the top stops.

Can someone take a look and explain why I’m having this problem? thanks.

C# script:

List<double> sort = new List<double>();
DA.GetDataList("Values", sort);
sort.Sort((a, b) => -1 * a.CompareTo(b));
DataTree<double> sorted = new DataTree<double>();
double max = 0.0;
DA.GetData("Max Sum", ref max);
List<double> exceeded = new List<double>();
int j = 0;

for (int i = 0; i < sort.Count; i++)
{
 if (sort[i] > max)
 {
   exceeded.Add(sort[i]);
   continue;
 }

 GH_Path path = new GH_Path(j);
 List<double> sumCheck = new List<double>();
 if (sorted.Branch(path) != null)
   sumCheck = sorted.Branch(path);
 else
   sumCheck.Add(0.0);
 
 if (sumCheck.Sum() + sort[i] <= max)
   sorted.Add(sort[i], path);
 else
 {
   for (int k =i+1;k<sort.Count;k++)
   {
     if (sumCheck.Sum() + sort[k] <= max)
     {
       sorted.Add(sort[k], path);
       sort.Remove(k);
       break;
     }

     if (k == sort.Count)
     {
       j++;
       sorted.Add(sort[i], path);
       break;
     }
   }
 }
}

DA.SetDataTree(0, sorted);
DA.SetDataList(1, exceeded);

Looking into it… in the meantime, inside Visual Studio I’d recommend using GH_Structure<GH_Number> instead of DataTree<double>. The datatree class was only added to expose trees within scripting components which didn’t have access to IGH_Goo like types, which GH_Structure demands.

Also since you’re doing this in VS, you should really put a breakpoint on your break; statement and then follow through what happens next. Debugging without breakpoints is basically like surgery over e-mail.

2 Likes

Ah, thanks for mentioning breakpoint, didn’t know there was such function.
turns out it’s the logic of my script that has problem, not break; (guess it’s no surprise there)

will take a look into GH_Structure

1 Like

Not sure about what are you trying to do… but if the goal is to cluster numbers according some condition (say their sum < some value) check attached.

Clusters_OnNumbers_entryLevel_V1.gh (115.8 KB)