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

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.

主站蜘蛛池模板: 边坝县| 东丰县| 舟山市| 奉节县| 合江县| 永顺县| 商南县| 尉犁县| 龙南县| 沅陵县| 盐池县| 渝中区| 安图县| 通化市| 电白县| 凯里市| 苍南县| 溧阳市| 洪雅县| 运城市| 玉林市| 滁州市| 朝阳县| 安远县| 大埔县| 和林格尔县| 阳曲县| 平安县| 辽源市| 闸北区| 当阳市| 潼南县| 长子县| 习水县| 邵东县| 龙泉市| 亳州市| 丹凤县| 南澳县| 抚远县| 宝丰县|