Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift if or/and statement like python

Is there a way to to do and/or in an if statement in swift. eg/

if a > 0 and i == j or f < 3:
    //do something 

can we do that in swift?

Thanks in advance

like image 437
Tomblasta Avatar asked Oct 19 '14 23:10

Tomblasta


People also ask

What does || mean in Swift?

If both the operands are non-zero, then the condition becomes true. (A && B) is false. || Called Logical OR Operator.

What is not equal to in Swift?

Swift supports the following comparison operators: Equal to ( a == b ) Not equal to ( a !=

What is guard in Swift?

In Swift, we use the guard statement to transfer program control out of scope when certain conditions are not met. The guard statement is similar to the if statement with one major difference. The if statement runs when a certain condition is met. However, the guard statement runs when a certain condition is not met.


2 Answers

You can use


&&

for logical and


|| 

for logical or


so you can do

if a > 0 && i == j || f < 3 {
    ...
}

see here https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/BasicOperators.html

like image 72
user3400223 Avatar answered Oct 16 '22 09:10

user3400223


Yes.

if (a > 0 && i == j || f < 3){
    //do something
}

You should probably do some reading on the basics of Swift before jumping in. If statements are covered in there.

like image 37
Connor Avatar answered Oct 16 '22 11:10

Connor