2

I can use the for in loop in Swift through this code

   for i in 0..<5
{
    print ("Four multiplied by \(i) results in \(i*4)" )
}

But how do i use the for with less than ">" condition.

  for i in 10>..5
{
    print ("Four multiplied by \(i) results in \(i*4)" )
}

It shows error: '>' is not a postfix unary operator for i in 10>..5

Ashley Rodrigues
  • 153
  • 2
  • 10
  • @EICaptainv2.0 this is not the c-style statement. Please understand the question and post comments – Ashley Rodrigues Jul 02 '16 at 05:58
  • @EICaptainv2.0 Hey Hey .... Nice attitude man... Posting meaningless comments and deleting afterwards. What man... ??? Its not right – Ashley Rodrigues Jul 02 '16 at 06:22
  • @EICaptainv2.0 Have a nice day man... – Ashley Rodrigues Jul 02 '16 at 06:24
  • @AshleyRodrigues and I am just trying to help you .. but you think if its meaningless than its ok ... – Bhavin Bhadani Jul 02 '16 at 06:25
  • @EICaptainv2.0 I appreciate your help. But i couldn't help myself when you posted my question as a duplicate of c-style syntax... Anyways glad to meet you.. – Ashley Rodrigues Jul 02 '16 at 06:28
  • @AshleyRodrigues may be its not duplicate ... but atleast you need to check ... if its wrong than no one gonna close your question .. thats it .. dont be so rude on such incident .. it happens in SO – Bhavin Bhadani Jul 02 '16 at 06:29
  • 2
    @AshleyRodrigues you don't have right to do rudely behave like this any people on SO. If it's not duplicate then you can simply tell the person like this is not duplicate. what is the meaning of "Please learn to comment wisely", "update yourself". please give respect to all the people. All people are trying to help each other. – Badal Shah Jul 02 '16 at 07:08

1 Answers1

7

Use the stride() method:

for i in 10.stride(to: 5, by: -1) {
     print ("Four multiplied by \(i) results in \(i*4)" )
}

This will increment through 10, 9, 8, 7, and 6. If you want 5 to be included, use through: instead:

for i in 10.stride(through: 5, by: -1) {
     print ("Four multiplied by \(i) results in \(i*4)" )
}
John Farkerson
  • 2,543
  • 2
  • 23
  • 33