23

Is there a way to write an If statement is Swift such as the following?

if a>b or c/d {
    //Do Something
}
4
  • 14
    There is absolutely no reason to downvote this question. Everyone has to start somewhere.
    – Rstew
    Commented Jun 19, 2015 at 1:00
  • 2
    Agreed with Stew. Also, you can do complex multiple conditions by using brackets to indicate order of operations as such: if (a==b && b==c) || (a==0 && c==1)
    – Max
    Commented Jun 20, 2015 at 9:40
  • 1
    some one know if i can.. this if number == (1, 2, 3) or maybe ranges of numbers ? if number == 1..3
    – marlonpya
    Commented Sep 14, 2017 at 15:57
  • @marlonpya no, you can't
    – user155
    Commented Mar 26, 2019 at 21:00

3 Answers 3

18

Just like everywhere:

if a > b || d % c == 0 {
   // do sth
}

I assume your c/d means you'd like d to be a multiple of c.

1
  • Sorry that was meant to be c>d or something of the like. Thanks so much though! That was quick and easy haha Commented Dec 13, 2014 at 20:29
12

Swift uses the same operators as all C-based languages, so

if a > b || c < d {
}

where || is the OR operator, && is the AND operator.

The list of all operators can be found in Swift Basic Operators

Not sure what c/d condition is supposed to mean.

0
3

for and operation we can use

if x==y && y == z { //perform some operations }

for or operation we can use

if x==y || y == z { //perform some operations }

Your Answer

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

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