3

How can we implement <= and >= operations in swift for loop. i tried stide operations no luck.

Below is the option I tried

for i in 1..<=3{
   print(i)
}

error: use of unresolved operator '..<=' for i in 1..<=3{

3
  • Can i use for i in 1..<(3+1){ print(i) }
    – Badrinath
    Jul 19, 2017 at 21:30
  • You mean for i in 1 ... 3 {}?
    – Hamish
    Jul 19, 2017 at 21:42
  • yes i wan the loop to be executed for '=' condition also
    – Badrinath
    Jul 19, 2017 at 21:46

2 Answers 2

18

You are looking for the “closed range operator”. It is spelled ...:

for i in 1 ... 3 {
    print(i)
}

You can read more about it under “Basic Operators” in The Swift Programming Lanugage.

1
8

in c / java:

for(int i=1 ; i<=3 ; i++)
    print(i)

in swift:

for i in 1 ... 3 {
    print(i)
}

in c / java:

for(int i=1 ; i<3 ; i++)
    print(i)

in swift:

for i in 1 ..< 3 {
    print(i)
}
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.