HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Operators

Operators

Math (+ - * / %), comparison (== != < >) and logical (&& || !) operators.

Simple enough for a 5-year-old to understand

Math operators

Division of two integers is an integer (7 / 2 = 3). At least one must be a Double for decimals.

Example code
1
print(7 + 3)
10
Output / Input10
2
print(7 / 2)
3 (integer division)
Output / Input3 (integer division)
3
print(7.0 / 2.0)
3.5 (decimal)
Output / Input3.5 (decimal)
4
print(7 % 2)
1 (remainder)
Output / Input1 (remainder)
Output / Input
💡 Hint: % gives the remainder; very commonly used for even/odd checks.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Operators are symbols that perform operations on values: addition, comparison, logical combination. In Swift, operators are also typed; the types they work with are defined, and an incompatible usage results in a compilation error.

Arithmetic operators are +, -, *, /, and % for remainder. Comparison operators like ==, !=, <, >, <=, and >= produce a Bool as a result. Logical operators && and || short-circuit: if the left side determines the result, the right side is not executed at all.

Two Swift-specific operators are worth learning. The ?? operator provides a fallback value if an Optional is nil. Range operators ..< and ... constantly appear in loops and slicing; the difference between the two is whether the upper bound is inclusive.

What you will learn

  • Arithmetic and comparison operators
  • Logical operators and short-circuiting
  • Compound assignment operators
  • Nil-coalescing operator
  • The difference between range operators

Who is this for?

  • Beginners who have learned basic syntax
  • Developers coming from other languages looking for ++
  • Those who encounter errors when writing conditions

When to use?

Comparison and logical operators are the foundation of any conditional statement; knowing the correct precedence reduces unnecessary parentheses.

The nil-coalescing operator is the shortest way to provide default values for missing fields coming from the server.

Common mistakes

Wrong

var count = 0
count++

The ++ and -- operators were removed from the language starting with Swift 3; this code produces the "'++' is unavailable" error. Apple removed these operators because they reduced readability and were error-prone. Compound assignment is used instead.

Correct

var count = 0
count += 1

Wrong

let a = 10
let b = 3
print(a % 2.5)

The % operator in Swift works only with integers; to get the remainder of floating-point numbers, the truncatingRemainder method is required. This code produces a type mismatch error. Floating-point remainder is separated as a distinct method because the definition of remainder in floating-point numbers differs from integers.

Correct

let a = 10.0
print(a.truncatingRemainder(dividingBy: 2.5))

Wrong

let max = Int8.max
let next = max + 1

Swift does not silently ignore numerical overflow; Int8 can hold at most 127, and this operation halts at runtime with "Swift runtime failure: arithmetic overflow". In many languages, the result silently wraps around to negative and the error goes unnoticed. In cases where overflow is acceptable, overflow operators are used.

Correct

let max = Int8.max
let next = max &+ 1

Frequently asked questions

Why was ++ removed?

The differing behaviors of prefix and postfix forms were a frequent source of bugs and reduced readability when used inside expressions. Writing += 1 is one character longer, but what it does is undeniably clear; the Swift team opted for clarity.

What is the difference between ..< and ...?

0..<5 produces the numbers 0,1,2,3,4; the upper bound is not included. 0...5 also includes 5. When working with array indices, ..< is almost always the correct one, because the last index is one less than the number of elements.

What does the ?? operator do?

If the Optional on its left has a value, it returns that value; if it is nil, it returns the fallback value on its right. For example, name ?? "guest" returns guest when name is nil. It is the safest and shortest alternative to forced unwrapping.

Summary

++ and -- do not exist in Swift, += 1 is used instead.

% only works with integers.

Numerical overflow is not silent, it halts at runtime.

?? provides a fallback value for Optionals.

..< excludes the upper bound, ... includes it.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code