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 Objective-C Basics (Retired) Functional Programming in C While & Do While Loop

Zach Perry
Zach Perry
1,303 Points

Differences between Loops

I recently viewed the lessons regarding the For Loop, While Loop, and Do While Loop. I seem to be having a difficult time understanding the differences between these 3 loops. I can understand the code that goes behind the loops a little bit, but not much. Can someone help me by shedding some light on these loops? Any advice or help is greatly appreciated. Thank you for your time

1 Answer

obey me
obey me
1,135 Points

Basically with the new language swift we have now 4 loops [for in ,while ,for ,do while ] loops but objective c only has 3 . Loop are basically task you create in a way of repeating a statement a number of times until some way of ending the loop occurs. Do while is basically saying that do this while this condition is true or false or your condition. while loop is saying do this task inside of the body you increment or decrement .

// how you represent them 

// for loop 

              for(int i =0; i <100;i++ ){

            // do whatever you want 
             // i is just a variable you could name it anything
             // i<100 saying that stop the task at 99 .
            // i++ means increment 

           }
// while loop 

                while (x<3)
                   {

                   x++;
              }

//do while loop 

                 do{
                  // do whatever you want 
                     x++;
                  }while (x<2);

Hope that help you

Michael Hulet
Michael Hulet
47,913 Points

Actually, for in does work in Objective-C. It looks like this:

NSMutableArray *numbers = @[1, 2, 3];
for(number in numbers){
    number++;
}

If anyone has anymore questions, just reply here

obey me
obey me
1,135 Points

True now i remember you lol Michael Hulet thanks for reminding me

I still dont understand why you would use over the other?

Michael Hulet
Michael Hulet
47,913 Points

for in is a lot better than a standard for loop because it not only does not require you to know the length of the object you're enumerating over beforehand, but it also gives you the object it finds for each iteration, so you can perform methods on it without having to find and retrieve it yourself