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
Alia Khan
3,733 PointsStruggling with loops in swift
Hi The topic of loops in swift is very confusing, as a beginner I feel like the challenges do not reflect the videos we watch. Can anyone explain loops in the simplest of form with links to any resources to understand them better? The apple documentation seems more apt for individuals who already have some knowledge on programming.
Also what is the best way to understand them? I know you should write your own code but its difficult to write code when you don't understand the function.
The syntax itself is simple and I understand that for the for in loops as well as the while and repeat while. The difficult part is the bit in the brackets and actually printing statements.
Thank you
2 Answers
Jason Anders
Treehouse Moderator 145,863 PointsHey Alia,
Once you get the hang of them, loops are pretty easy. Explaining them without examples... not so much.
What I recommend for you is to take the JavaScript Course on Loops.
Sometimes all you need is a different perspective or new way of explaining to have that "Ah-ha" moment. I started with HTML and JavaScript before moving into the more complex languages (like Objective-C or Swift), so even though the syntax is a bit different, Dave is a great teacher. Give it a shot and see if it makes loops a bit more clear.
Keep Coding! :)
Daniel Cunningham
21,109 PointsHello Alia, At it's simplest concept, a loop is a means to take a repetitive task and reduce it to a single (or couple) lines of code. As a 'non code' example, consider the process of buying items at a grocery store. When you are at the check out line with 20 items, you know that you'll need to scan each item individually in order to arrive at a total basket price. After you have your total, you pay and leave the store, an action that is performed only once. In the absense of a looping function, you would have to initiate a command like "scanItem(item1), scanItem(item2), scanItem(item3)..." 20 times until you reach the end of your basket. That's, quite frankly, a pain to do and very inefficient.
Enter the loop. Keeping wth the grocery store example, you could think of your basket of items as an Array. In order to logically scan the items in order, you need to create some sort of 'object' that will select a single item, scan it, and then move on to the next item within the array. consider the following:
var basket: [String] = ["Milk", "Cookies", "Sugar", "Chips", "Yogurt"]
for item in basket {
print(item)
}
'Basket' represents your shopping cart. 'item' becomes a means of selecting a single object within a basket and the statement inside will then print each 'item' individually. All of this is done with a single looping statement rather than requiring me to write print 5 times with indexing. Additionally, this statement is dynamic because an array can be appended, so if another 1 or 1 million items are added to it, the statement will continue to run until it has printed everything in the array.
I hope that helps! Good luck coding!