Understanding Swift - Boolean


In Swift, the Boolean data type (Bool) is used to represent logical values—either true or false. Booleans are essential for decision-making and control flow in programming, allowing you to implement conditions and logic in your code.

Syntax of Boolean in Swift

Standard Syntax:

let value1: Bool = true
let value2: Bool = false

Shorthand Syntax:

let value1 = true
let value2 = false

Example 1: Using Boolean in Conditional Statements

import Foundation

// Defining a Boolean variable
let isRedColor: Bool = true

// Checking the condition
if isRedColor {
print("My car color is red")
} else {
print("My car color is not red")
}

Output

My car color is red