Tutorial by Examples

Bool is a Boolean type with two possible values: true and false. let aTrueBool = true let aFalseBool = false Bools are used in control-flow statements as conditions. The if statement uses a Boolean condition to determine which block of code to run: func test(_ someBoolean: Bool) { if som...
The prefix ! operator returns the logical negation of its argument. That is, !true returns false, and !false returns true. print(!true) // prints "false" print(!false) // prints "true" func test(_ someBoolean: Bool) { if !someBoolean { print("someBoolean ...
The OR (||) operator returns true if one of its two operands evaluates to true, otherwise it returns false. For example, the following code evaluates to true because at least one of the expressions either side of the OR operator is true: if (10 < 20) || (20 < 10) { print("Expression...
A clean way to handle booleans is using an inline conditional with the a ? b : c ternary operator, which is part of Swift's Basic Operations. The inline conditional is made up of 3 components: question ? answerIfTrue : answerIfFalse where question is a boolean that is evaluated and answerIfTrue...

Page 1 of 1