21

I'm trying to skip in a random condition that if true, will skip a loop, but results are printing all elements 0,1,2,3,4 I know in Java if index is incremented index will skip, but this is not happening Swift.?

Update: this is a simplified version of some program I wrote, the print() has to be happen right after each loop, and index is ONLY incremented in some unknown conditions, I want it to behave like JAVA.

for var index in 0..<5 {
  print(index)//this prints 0,1,2,3,4, this must happen right after for loop
  if some unknown condition {
      index+=1
  }
}
4
  • 1
    You can use 'continue' with if clause like if index == 2 then continue!! Commented Jan 9, 2017 at 9:55
  • I don't understand what you want to do, because you want to check if an unknown condition is true... What do you want? A random number generator which will return which index should be removed?? Please be more clear. Commented Jan 9, 2017 at 9:57
  • I have a similar problem. My "unknown" condition is found inside the for-loop to determine whether the next iteration is skipped or not. I turned out declare a new variable to do the job Commented Dec 6, 2017 at 0:55
  • the second method I used is by while-loop, instead of for-loop. In that way the index is incremented as you want Commented Dec 6, 2017 at 1:18

7 Answers 7

48

The index is automatically incremented in the loop, you can skip an index with a where clause:

for index in 0..<5 where index != 2 {
    print(index)
}
Sign up to request clarification or add additional context in comments.

4 Comments

@vandian : best answer among all :) Hence up vote :)
what if $0 != 2 is not a known condition?
@miracle-doh simply replace index != 2 with the condition you'd like to test (eg. for index in 0..<5 where myCondition(index) { ... })
what if you're looping through objects that dont have a compareable interface I just need to skip the first index of my list. In something like java I could just start the loop at for(int i = 1 ... instead of i = 0
36

Please try this one:

for var index in 0..<5 {

  if index == 2 {
     continue
  }
  print(index)//this prints 0,1,3,4
}

2 Comments

maybe without "2"(in the comment)
@ItsMeDom ... Thanks a lot
5

This works. Not sure if it is the best way to do this.

var skip = false
for var index in 0..<5 {
  if (skip) {
    skip = false
    continue
  }

  print(index)
  if index == 2 {
      skip = true
  }
}

Comments

1

In your for-loop:

for var index in 0..<5 {
if index != 2{
   print(index)
}
}

Comments

0
for var index in 0..<5 {

  if index == 2 {
      //dont do anything
  }
  else {
     print(index)
  }
}

Isn't it that simple ??

1 Comment

well, if your upperlimit is 100million, it would be nice to simply skip to the number
0
(0..<5).filter({$0 != 2}).forEach{ x in print(x)}

(or) when the condition is unknown,

(0..<5).filter(isNumber2).forEach{ x in print (x)}

Note: isNumber2 is an external function that returns the boolean after evaluating the condition on the number. Your implementation of this function could be different.

var isNumber2: (Int) -> (Bool) = { return $0 != 2}

1 Comment

what if $0 != 2 is not a known condition?
0

for index in 0 ..< 5 where (give your condition here) { print(index) }

If the condition is to skip index 3, for index in 0 ..< 5 where index != 3 { print(index) }

1 Comment

@miracle-doh i dont think the boolean variable is needed to perform something simple as this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.