Programming in Swift - Part 13: Part 2: Flow Control: | Ray Wenderlich

Learn how to use for loops in Swift, along with ranges, continue, and labeled statements.

This is a companion discussion topic for the original entry at

hi @rwenderlich thanks for the tutorial. I wrote a code like this

let arrayNumber = [1,2,3,4]

func calculate(x:Int) → Int {

var z = 0

for i in arrayNumber {
    z = i + x

return z


calculate(x: 1) // result = 5

I don’t understand why the result of this function is 5, I expect (1+1) + (2+1) + (3+1) + (4+1) = 14

what went wrong in here ?

Thanks in advance :slight_smile:

I can tell you what went wrong there, but one tip that can help you figure this out for yourself - running code like this in a playground is an easy way to see how the code executes over loops and to see why a calculation might not go the way you think it might … Also, putting NSLog (or print) statements in your code can help too :smiley:

As far as what went wrong, your z value gets reset for each iteration of the loop. So, you only get the results of the last calculation - 4 + 1 when the method exits. If you wanted the total from all the loops, then you need to change this line:

z = i + x


z += i + x

Hope that helps :slight_smile:

oh my God, a silly mistake from a beginner :sob:

Thank you very much for your clear explanation, @fahim .

actually I have problem in part 40 in programming in swift. same problem, in for loop syntax. Thats why i went back to this part.

struct Location {
let x : Int
let y : Int

struct DeliveryArea {
let coordinate : Location
let rangeDistance : Double

let locationRestaurant1 = Location(x: 2, y: 4)
let restaurant1 = DeliveryArea(center: locationRestaurant1, rangeDistance: 3)

let locationRestaurant2 = Location(x: 5, y: 9)
let restaurant2 = DeliveryArea(center: locationRestaurant2, rangeDistance: 2)

let restaurantsArray = [restaurant1,restaurant2]

func distance (from source: Location, to target: Location) → Double {
let distanceX = Double(source.x - target.x)
let distanceY = Double(source.y - target.y)
return sqrt(distanceX * distanceX + distanceY * distanceY)

func isInDeliveryRange (location: Location) → Bool {

for restaurant in restaurantsArray {
    let deliveryDistance = distance(from: location, to: restaurant.coordinate)
    if deliveryDistance > restaurant.rangeDistance {
        return false

return true


there are 2 elements in restaurantsArray (restaurant1 and restaurant2), and restaurantArray will be used in the for loop inside isInDeliveryRange() function.

but when i call isInDeliveryRange function, the return value is only 1 (false)

isInDeliveryRange(location: (20,3)) // result : false

I expect in result is 2 boolean value since we have 2 elements in the restaurantsArray




When you call return from within a loop, it immediately exits the loop (and the method). So even though you are iterating over an array with multiple elements, if it hits the return within the if block in the loop, it will exit after that item instead of completing the loop iteration.

If you want to see the results for each of the restaurants as an array, then you will have to modify your code to return an array of booleans instead of a single boolean returned from your method. I don’t know if that is what you’re looking for, but if it is, you would need to modify your isInDeliveryRange method to look something like this:

func isInDeliveryRange (location: Location) -> [Bool] {
	var result = [Bool]()
	for restaurant in restaurantsArray {
		let deliveryDistance = distance(from: location, to: restaurant.coordinate)
		let inRange = deliveryDistance <= restaurant.rangeDistance
	return result
1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.