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 trialZach Perry
1,303 PointsDifferences 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
1,135 PointsBasically 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
47,913 PointsMichael Hulet
47,913 PointsActually,
for in
does work in Objective-C. It looks like this:If anyone has anymore questions, just reply here
obey me
1,135 Pointsobey me
1,135 PointsTrue now i remember you lol Michael Hulet thanks for reminding me
Iosif Skorohodov
1,126 PointsIosif Skorohodov
1,126 PointsI still dont understand why you would use over the other?
Michael Hulet
47,913 PointsMichael Hulet
47,913 Pointsfor in
is a lot better than a standardfor
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