I am trying to build a basic fibonacci sequence using a while loop in Swift.
The condition I am using in the while loop is while var next <= var maxNum, where next is an integer containing the newest element in the array to be appended, and maxNum is an integer that represents the largest element to be contained in the array (to test the while loop, I hardcoded it to ten).
Getting the following error when running the below code in playground: "Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"
The while loop runs 90 times before this happens, letting me know that my condition is breaking down...var next should be greater than 10 after a few loops....not sure what's going on.
import UIKit
var myArray = [0,1]
var maxNum = 10
var next = 0
while next <= maxNum{
var last = myArray.last!
var lastLast = myArray[myArray.count-2]
var next = last + lastLast
myArray.append(next)
}
println(myArray)
println(myArray.last!)
nextvariable inside the loop? That would cause the outernextto remain 0.