Values in Swift

Swift has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

import Foundation

// Strings, which can be added together with +.
print("swift" + "lang")

// Integers and floats.
print("1+1 =", 1 + 1)
print("7.0/3.0 =", 7.0 / 3.0)

// Booleans, with boolean operators as you'd expect.
print(true && false)
print(true || false)
print(!true)

To run the program, save it as values.swift and use the Swift compiler:

$ swift values.swift
swiftlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In Swift, we don’t need to define a main() function as the entry point of our program. The code is executed from top to bottom.

Swift uses print() instead of fmt.Println() for console output. The print() function in Swift can handle multiple arguments and automatically adds spaces between them.

For string concatenation, Swift uses the + operator, similar to many other languages.

Arithmetic operations in Swift are straightforward and similar to other C-style languages.

Boolean operations in Swift use the same operators as in many other programming languages: && for AND, || for OR, and ! for NOT.

Swift is a type-safe language, but it also has type inference, which means you don’t always need to explicitly declare the type of a variable or constant.