Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

What is a Boolean ?

I already finished swift basics but i still can't quite understand the function of a boolean.

David Richardson
David Richardson
10,213 Points

Boolean is a data type that only has 2 possible values, either true or false (1 or 0)

2 Answers

The concept of a bool is pretty simple, think of it as a condition that is true or false.

For example.

/*
 Are you human ? There are two posible answers to the question ( true or false ) 
 therefore this can be consider a boolean. Lets see it in code.  
*/

var human = true 

if human == true {
println("Yes this is true")
} else {
println("No this is false")
}
// This will print “Yes this is true” because the variable human
// was declared with the value "true"

/*

You can read that as If human is true, then print “Yes this is true”, if it’s false, print “No this is false” So the condition of something being true or false is a boolean. It only has those two answers.

Hope you understand.

Austin Liu
Austin Liu
3,326 Points

A boolean is a value that either responds in 'true' or 'false', or in simpler terms, 'yes' or 'no'. Booleans can be used in asking if, suppose if a user is connected to the internet, or if they like cheese, or something along those lines. Hope this helped!