Could someone explain: 1. Warning (CS0162): Unreachable code detected (line 90)

Hey guys,

So, I am fairly new in coding, Rhinoceros and Grasshopper. I took an introductory course as part of a class in my Masters and I thought I was grasping the basics but then this error comes out: 1. Warning (CS0162): Unreachable code detected (line 90). Since I have no background in coding/programing I have been googling and trying to understand what that means. Honestly, I can not follow the explanations online, they are too complex.

What I am trying to do is simple: I want to be able to create random extrusions in which the height depends on the distance to reference point. The closer to the point the higher the extrusion.

I attached my GH file here if everyone can check on Line 90 and explain to me what does the Warning mean.

Thank you.

PS: if anyone knows about some good C# beginners crash courses I am open ears or eyes.

UMS_Dario Corral.gh (4.1 KB)

You have a for loop that will never loop because you have a break; at the end. This means the increment will never happen. In short:

for(int i = 0; i < 10; i++) {
    // code here
    break; // <<- this break just before the end of the loop
           // causes the i++ to never be run -> unreachable code.
}

Get rid of the inner foreach over rectangles, and instead read Rectangle3d rectangle = rectangles[i];. That will get the rectangle for each midpoint you created earlier.

    for(int i = 0; i < rectangles.Count; i++)
    {
      Rectangle3d rectangle = rectangles[i];
      double numerator = (random.Next(5, 25)) * 1000;
      double denominator = distances[i];

      double height = numerator / denominator;

      NurbsCurve curve = rectangle.ToNurbsCurve();
      Extrusion extrusion = Extrusion.Create(curve, height, true);
      extrusions.Add(extrusion);
    }
1 Like

Hey Nathan,

Thanks for clearing up the explanation and the tip. That will help me so much in the future. I have been trying different small codes and I always ended up trying to do a foreach + for loop. I had no idea I could do it as you mentioned. By the way it worked perfectly. Now I just have to adjust my dimensions in the code to make it look better.

Thank you again for your quick answer.
Dario