← Back to Swift Course | Chapter 2: Variables & Types | Lesson 6 of 8

Booleans

A boolean is a value that can only ever be true or false, like answering yes or no to a question.
Syntax
swift
let name: Bool = true
let result = a >= b            // == != < > <= >=
let combined = x && y || !z

Declaring Booleans

A Bool variable holds either true or false, often as the result of a comparison.

Example: Declaring Booleans

markup
// Declare a constant `isSwiftFun`, set to `true`
let isSwiftFun = true
// Declare a constant `isDifficult`, set to `false`
let isDifficult = false
// Print "Is Swift fun? \(isSwiftFun)" to the console
print("Is Swift fun? \(isSwiftFun)")
// Print "Is it too difficult? \(isDifficult)" to the console
print("Is it too difficult? \(isDifficult)")

Comparison Operators Produce Booleans

Comparing two values with ==, !=, <, >, <=, or >= always produces a Bool result.

Example: Comparison Operators Produce Booleans

markup
// Declare a constant `age`, set to `20`
let age = 20
// Declare a constant `isAdult`, set to `age >= 18`
let isAdult = age >= 18
// Print "Is an adult: \(isAdult)" to the console
print("Is an adult: \(isAdult)")

Combining Booleans with Logical Operators

The && (logical AND), || (logical OR), and ! (logical NOT) operators combine or invert boolean values to build more complex conditions.

Example: Combining Booleans with Logical Operators

markup
// Declare a constant `hasTicket`, set to `true`
let hasTicket = true
// Declare a constant `hasID`, set to `false`
let hasID = false
// Declare a constant `canEnter`, set to `hasTicket && hasID`
let canEnter = hasTicket && hasID
// Print "Can enter: \(canEnter)" to the console
print("Can enter: \(canEnter)")
// Print "Missing ID: \(!hasID)" to the console
print("Missing ID: \(!hasID)")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Comparing a boolean to true explicitly, e.g. if isReady == true, instead of the cleaner if isReady.
  2. Assuming any non-zero number counts as true like in C; Swift's Bool is a distinct type and numbers are never implicitly converted to booleans.
  3. Forgetting that && and || short-circuit, so a later expression might not even be evaluated.
Chapter Summary
  • Bool has exactly two values: true and false.
  • Comparison operators (==, <, >, etc.) produce Bool results.
  • Logical operators && (and), || (or), and ! (not) combine boolean values.
  • Unlike C, Swift never treats numbers as implicitly true or false.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.