StefanN
StefanN

Reputation: 135

swift3 for loop with two counter

My question seams to be quite easy but currently I am not seeing the wood for the trees.

I've written code in swift 2 using a for-loop with two counter like:

for var i = 0, j = 1; i < 5; i++, j++ {
 code...
}

but, with swift 3 this become deprecated.

I mean with one variable it's clear:

for i in 0 ..< endcondition {
  code...
}

but how would the code with a for-loop looks like in swift 3 with two counter, without interlacing two for-loop?

Thanks in advance Stefan

Upvotes: 0

Views: 921

Answers (2)

Jeff Brown
Jeff Brown

Reputation: 86

var i:Int = 0
var j:Int = 1
for each in 0...arrayCount -1{
    //do stuff
    /// place increments in closures as needed
    j += 1
    i += 1
}

You may want to consider on while loop to allow usage of evaluating multiple values to signal a stop.

while condition {
      statements
      Change condition
 }

Upvotes: 0

vadian
vadian

Reputation: 285072

In this particular case

for i in 0..<5 {
  let j = i + 1
  print(i, j)
}

Upvotes: 2

Related Questions