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 that accepts one of the two built-in Boolean constants: true and false.

Boolean constants and variables can be defined like this:

let swiftIsCool = true  
var itIsRaining = false 

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

let isSwiftCool = true  
let isItRaining = false  
if isSwiftCool { 
  print("YEA, I cannot wait to learn it") 
} 
if isItRaining {  
  print("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. This line is printed out because the isSwiftCool Boolean type is set to true. As the is ItRaining variable is set to false, the Get a rain coat message is not printed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset