Introduction to Swift

Apr 24 2024 · Swift 5.10, iOS 17, Xcode 15

Lesson 02: Flow Control & Functions

Loops Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll improve the code you wrote last lesson for the Fibonacci sequence to use loops.

var fibonacciSeries: [Int] = []
for index in 0...10 {

}
if index == 0 {
  fibonacciSeries.append(0)
} else if index == 1 {
  fibonacciSeries.append(1)
}
else {
  let element1 = fibonacciSeries[fibonacciSeries.count-1]
  let element2 = fibonacciSeries[fibonacciSeries.count-2]
  fibonacciSeries.append(element1 + element2)
}
print(fibonacciSeries)
if index < 2 {
  fibonacciSeries.append(index)
}
See forum comments
Cinema mode Download course materials from Github
Previous: Using Loops Next: Functions