Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greater than and Less than equal in for loop on swift 4

Tags:

for-loop

swift

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{

like image 832
Badrinath Avatar asked Dec 08 '25 00:12

Badrinath


2 Answers

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.

like image 58
rob mayoff Avatar answered Dec 10 '25 20:12

rob mayoff


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)
}
like image 20
Samad Avatar answered Dec 10 '25 20:12

Samad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!