官术网_书友最值得收藏!

The Boolean type

Boolean values are often referred to as logical values because they can be either true or false. Swift has a built-in Boolean type called Bool that accepts one of two built-in Boolean constants. These constants are true and false.

Boolean constants and variables can be defined like this:

let swiftIsCool = true
let swiftIsHard = false

var itIsWarm = false
var itIsRaining = true

Boolean values are especially useful when working with conditional statements, such as if and while. For example, what do you think this code would do:

let isSwiftCool = true
let isItRaining = false
if (isSwiftCool) {
    println("YEA, I cannot wait to learn it")
}

if (isItRaining) {
    println("Get a rain coat")
}

If you answered that this code would print out YEA, I cannot wait to learn it, then you would be correct. Since isSwiftCool is set to true, the YEA, I cannot wait to learn it message is printed out, but isItRaining is false; therefore, the Get a rain coat is not.

You can also assign a Boolean value from a comparison operator like this:

var x = 2, y = 1
var z = x > y

In the preceding code, z is a Boolean variable containing a Boolean true value.

主站蜘蛛池模板: 潮安县| 杭锦后旗| 永年县| 方正县| 莎车县| 东方市| 泰安市| 抚顺县| 呈贡县| 汾西县| 中卫市| 朝阳县| 施甸县| 高邑县| 新干县| 波密县| 馆陶县| 昂仁县| 铜陵市| 日喀则市| 内丘县| 丹东市| 高密市| 梁平县| 浪卡子县| 临潭县| 昌吉市| 合水县| 富锦市| 丰城市| 龙泉市| 漳浦县| 崇明县| 万载县| 永吉县| 荃湾区| 高雄县| 内江市| 万州区| 枣庄市| 翼城县|