HELP! I don't understand Loops.

Hello,

I have a big problem because I cannot ever seem to understand loops. The specific is how to use them effectively and how to use them overall. I sorta understand the syntax. I looked over the swift language guide and tried to practice on Code Academy but It won't get stuck in my head and I am so confused.

Imran

Answered by Claude31 in 683501022

It is in fact pretty simple. The best is to try in playground

The basic loop is the for loop, where you iterate over elements.

 1. Index are just sequential index in a range

for i in 0...9 {
  print(i)
}

You could define this range in a var

var zeroToNine = 0...9
for i in zeroToNine {
  print(i)
}

Range can come in other flavours, as open range:

for i in 0 ..< 10 {
  print(i)
}

This is useful when you iterate on the elements in an array

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for i in 0..<myArray.count {
  print(i)
}

In those examples, you used the index i. But in some cases you don't need:

for i in 0 ..< 10 {
  print("Hello")  // i is not used
}

Then you don't need to declare i

for _ in 0 ..< 10 {
  print("Hello")  // i is not used
}

 2. You can also iterate directly on elements in an Array (in fact on elements of any collection)

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for item in myArray {
  print(item)
}

This is equivalent to

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for i in 0..<myArray.count {
  print(myArray[i])
}

 3. If you don't want to use all items, but just some, use stride

for oddNum in stride(from: 1, to: 5, by: 2) {
  print(oddNum)
}

There are other loops very useful when you have to test a condition: do while

 4. repeat while, to test condition at the end

var nn = 0
repeat {
    print(nn)
    nn += 2
    //take input from standard IO into variable n
} while nn < 10

 5. while where you test at start, before executing the code in brackets

var nn = 0
while nn < 10 {
    print(nn)
    nn += 2
    //take input from standard IO into variable n
}

See the subtle difference between 4 and 5 by testing the following:

var nn = 10
repeat {
    print(nn)
    nn += 2
} while nn < 10

nn = 10
while nn < 10 {
    print(nn)
    nn += 2
}

Hope that helps.

What is it more precisely you don't understand ?

It it the for loop, the while loop ? Is it something specific in the syntax ?

Please give examples of what you don't understand, we'll try to help.

Note: you should use Apple book:

  • intro to App development with Swift

or, more detailed:

  • The Swift programming language.

They are much more didactic and will ease your understanding.

Accepted Answer

It is in fact pretty simple. The best is to try in playground

The basic loop is the for loop, where you iterate over elements.

 1. Index are just sequential index in a range

for i in 0...9 {
  print(i)
}

You could define this range in a var

var zeroToNine = 0...9
for i in zeroToNine {
  print(i)
}

Range can come in other flavours, as open range:

for i in 0 ..< 10 {
  print(i)
}

This is useful when you iterate on the elements in an array

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for i in 0..<myArray.count {
  print(i)
}

In those examples, you used the index i. But in some cases you don't need:

for i in 0 ..< 10 {
  print("Hello")  // i is not used
}

Then you don't need to declare i

for _ in 0 ..< 10 {
  print("Hello")  // i is not used
}

 2. You can also iterate directly on elements in an Array (in fact on elements of any collection)

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for item in myArray {
  print(item)
}

This is equivalent to

let myArray = ["A",  "B",  "C", "D", "E", "F"]
for i in 0..<myArray.count {
  print(myArray[i])
}

 3. If you don't want to use all items, but just some, use stride

for oddNum in stride(from: 1, to: 5, by: 2) {
  print(oddNum)
}

There are other loops very useful when you have to test a condition: do while

 4. repeat while, to test condition at the end

var nn = 0
repeat {
    print(nn)
    nn += 2
    //take input from standard IO into variable n
} while nn < 10

 5. while where you test at start, before executing the code in brackets

var nn = 0
while nn < 10 {
    print(nn)
    nn += 2
    //take input from standard IO into variable n
}

See the subtle difference between 4 and 5 by testing the following:

var nn = 10
repeat {
    print(nn)
    nn += 2
} while nn < 10

nn = 10
while nn < 10 {
    print(nn)
    nn += 2
}

Hope that helps.

HELP! I don't understand Loops.
 
 
Q