Chapter 2: Basic Syntax
- arda doğantemur
- May 5, 2023
- 2 min read
Updated: May 6, 2023

Variables and Constants
In Swift, variables and constants are used to store values that can be used throughout the program. Variables are used to store values that can change, while constants are used to store values that cannot change. Here's an example of how to declare a variable and a constant in Swift:
var myVariable = 42
let myConstant = 3.14In this example, myVariable is a variable that stores an integer value of 42, while myConstant is a constant that stores a floating-point value of 3.14.
Data Types
Swift is a strongly typed language, which means that each variable and constant must have a specific data type. Here are some of the most common data types in Swift:
Int: Used to store integer values, such as 42 or -12.
Double and Float: Used to store floating-point values, such as 3.14 or -0.5.
Bool: Used to store Boolean values, which are either true or false.
String: Used to store text values, such as "Hello, world!".
Here's an example of how to declare a variable with a specific data type:
var myInteger: Int = 42In this example, myInteger is a variable of type Int that stores the value 42.
Operators
Operators are used in Swift to perform calculations or comparisons. Here are some of the most common operators in Swift:
Arithmetic Operators: Used to perform basic arithmetic calculations, such as addition, subtraction, multiplication, and division. For example:
var sum = 5 + 3
var difference = 5 - 3
var product = 5 * 3
var quotient = 5 / 3Comparison Operators: Used to compare values, such as equal to, not equal to, greater than, and less than. For example:
var isEqual = 5 == 3
var isNotEqual = 5 != 3
var isGreaterThan = 5 > 3
var isLessThan = 5 < 3Logical Operators: Used to combine Boolean values, such as && (logical AND), || (logical OR), and ! (logical NOT). For example:
var isTrue = true
var isFalse = false
var logicalAnd = isTrue && isFalse
var logicalOr = isTrue || isFalse
var logicalNot = !isTrue
Control Flow Statements
Control flow statements are used in Swift to control the flow of execution in a program. Here are some of the most common control flow statements in Swift:
If Statement: Used to perform a block of code if a certain condition is true. For example:
var myNumber = 42
if myNumber > 50 {
print("My number is greater than 50")
} else {
print("My number is less than or equal to 50")
}For Loop: Used to perform a block of code a certain number of times. For example:
for i in 0..<5 {
print("The value of i is \(i)")
}While Loop: Used to perform a block of code while a certain condition is true. For example:
var i =0
while i <5
{
print("The value of i is \(i)")
i +=1
}In this chapter, we covered the basic syntax of Swift, including variables, constants, data types, operators, and control flow statements. Understanding these fundamental concepts is essential for any Swift developer, as they form the building blocks of the language. In the next chapter, we'll dive deeper into data types and explore more advanced topics, such as optionals and type inference.


Comments