Conditions and While Loops
Boolean Operators
Or
Use the ||
infix operator to use an inclusive or operator between two booleans
Both or
and and
operators in Swift are short-circuit meaning that the operator will operate on its parameters until it has guaranteed result in which case it will return. This means, in some cases, they will not operate both conditions.
Due to short-cuircuit rules, we would expect this to evaluate to true
. When an or
operator reaches an expression that evaluates to true
, it can stop evaluation because no matter what the other expression is, it will be true
. Thus, it will never encounter the error.
I slightly lied... This is actually a special case because the compiler knows to look for division by 0, so this actually does not compile (but if we ran it, it would evaluate to true
). We can silence this error by hiding the fact that we are dividing by 0 with a function:
This compiles and runs without error. Although, if we used false
instead of true
for the first expression, the second expression would need to be evaluated, and the program would then throw an error.
You may think this is an odd piece of dynamic symantics (how code runs) to analyze, but this can produce puzzling behavior in object oriented programming. If the expressions change the state of your program, this is something you must keep in mind.
And
And
is similar to the or
operator–it's a short-circuit operator. But, we use the &&
symbol instead.
In contrast, the and
operator can only short-circuit if the first expression evaluates to false
.
Not
Unlike or
and and
, not
is a unary prefix operator. This means that we can apply it to a singular boolean expression by placing an exclamation point (or bang) !
in front of the expression:
Control Flow
If-Else Statements
If-else statements are standard in Swift. You just write if
, followed by an expression, followed by an execution block. If you want, you can follow this with an else
statement or an else if
if you want another condition.
Ternary
Ternary, for 3 expressions, can be though of as in-place if-else expression. Ternaries are written as <insert boolean expression> ? <expression 1> : <expression 2>
. This only works if expression 1
and expression 2
have the same type. If the boolean is true
, expression 1
is returned, otherwise, expression 2
is returned.
Toggle
You often use booleans to store states. In these cases, you will want to be toggling this state on and off. You could write that like this:
We could also do this with a ternary:
We could even do it in fewer lines with not
:
But actually we don't need to do any of these because is already done in Swift. If we want to toggle a boolean, we can simply use the toggle method.
While
While loops exactly what you'd expect them to be. A loop the executes while some expression evaluate to be true
, with the syntax while <insert expression> { <code block> }
. Just make sure you're making progress towards termination!
Last updated