Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

bolo Michelin
bolo Michelin
4,645 Points

loop in swift

Hi Guys

I have tried to port a loop in C to swift

My loop in C

for (iG7 = 35; iG7 <= 63; iG7++) { // Constraint 2)

    for (iG8 = 8; iG8 <= 52; iG8++) {                                    // Constraint 3)

        G8 = (float)(iG8);
        for (iG9 = 3; iG9 <=46; iG9++) {                                   // Constraint 7)


            for (iG10 = 10; iG10 <= 53; iG10++) {                            // Constraint 4)

                G10 = (float)(iG10);

            }
        }
    }
}

my loop in swift

    for iPM in 35...63
    {
       for iSM1 in 8...52
        {
                          for iSM2 in 8...46
            {

                for iSM3 in 8...53
                {

                }
            }
        }
    }

I have two questions

  1. i can compile this loop in swift. Xcode crashed.
  2. How can improve the code ? Do you have any suggestion

Bolo

1 Answer

Hi Bolo,

From what you have provided it is very unclear what you want the loop to do as it will run for some time before completion which may be what is causing xCode to crash.

If you can shed some light on what it is that this loop does other students and I may be able to provide more help.

Based of your C loop I constructed this in my playground:

var iG8, iG9, iG10 : Int?
var G8, G10 : Float?

for iG8 in 8...52{
    G8 = Float(iG8)  //Ran 45 times
    for iG9 in 3...46{
      for iG10 in 10...53{
        G10 = Float(iG10) //Ran 87120 times
      }
    }
}
"G8 is \(G8!) & G10 is \(G10!)"  //After running G8 is 52.0 & G10 is 53.0"

Again, if you would like help to make this loop more efficient then please provide some more information on it.