In this section, we will cover the fundamental syntax and structure of Swift programming. Understanding these basics is crucial as they form the foundation for writing any Swift program.
Key Concepts
- Comments
- Printing to the Console
- Semicolons
- Variables and Constants
- Data Types
- Basic Operators
- Control Flow Basics
- Comments
Comments are used to annotate code and are ignored by the compiler. Swift supports both single-line and multi-line comments.
- Printing to the Console
The print
function is used to output text to the console.
- Semicolons
In Swift, semicolons are optional at the end of statements. However, if you want to write multiple statements on a single line, you must use semicolons.
- Variables and Constants
Variables and constants are used to store values. Variables are declared using the var
keyword, and constants are declared using the let
keyword.
Example:
- Data Types
Swift is a type-safe language, meaning it ensures that variables are always used with the correct type. Common data types include:
Int
for integersDouble
andFloat
for floating-point numbersBool
for boolean valuesString
for text
Example:
var integer: Int = 10 var floatingPoint: Double = 3.14 var boolean: Bool = true var text: String = "Swift"
- Basic Operators
Swift supports various operators for performing arithmetic, comparison, and logical operations.
Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ |
Addition | 2 + 3 |
- |
Subtraction | 5 - 2 |
* |
Multiplication | 3 * 4 |
/ |
Division | 10 / 2 |
% |
Modulus | 10 % 3 |
Example:
let sum = 2 + 3 let difference = 5 - 2 let product = 3 * 4 let quotient = 10 / 2 let remainder = 10 % 3
Comparison Operators
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 |
!= |
Not equal to | 5 != 3 |
> |
Greater than | 5 > 3 |
< |
Less than | 3 < 5 |
>= |
Greater or equal | 5 >= 5 |
<= |
Less or equal | 3 <= 5 |
Example:
let isEqual = (5 == 5) let isNotEqual = (5 != 3) let isGreater = (5 > 3) let isLess = (3 < 5) let isGreaterOrEqual = (5 >= 5) let isLessOrEqual = (3 <= 5)
Logical Operators
Operator | Description | Example |
---|---|---|
&& |
Logical AND | true && false |
` | ` | |
! |
Logical NOT | !true |
Example:
- Control Flow Basics
Control flow statements are used to control the execution of code based on certain conditions.
If Statement
let score = 85 if score >= 90 { print("Grade: A") } else if score >= 80 { print("Grade: B") } else { print("Grade: C") }
For Loop
While Loop
Practical Exercise
Exercise 1: Basic Arithmetic
Write a Swift program that calculates the sum, difference, product, and quotient of two numbers and prints the results.
Solution:
let num1 = 8 let num2 = 4 let sum = num1 + num2 let difference = num1 - num2 let product = num1 * num2 let quotient = num1 / num2 print("Sum: \(sum)") print("Difference: \(difference)") print("Product: \(product)") print("Quotient: \(quotient)")
Exercise 2: Control Flow
Write a Swift program that checks if a number is positive, negative, or zero and prints the result.
Solution:
let number = -3 if number > 0 { print("The number is positive.") } else if number < 0 { print("The number is negative.") } else { print("The number is zero.") }
Conclusion
In this section, we covered the basic syntax and structure of Swift, including comments, printing to the console, variables, constants, data types, basic operators, and control flow basics. These fundamentals are essential for writing any Swift program. In the next section, we will delve deeper into variables and constants, exploring their usage and importance in Swift programming.
Swift Programming Course
Module 1: Introduction to Swift
- Introduction to Swift
- Setting Up the Development Environment
- Your First Swift Program
- Basic Syntax and Structure
- Variables and Constants
- Data Types
Module 2: Control Flow
Module 3: Functions and Closures
- Defining and Calling Functions
- Function Parameters and Return Values
- Closures
- Higher-Order Functions
Module 4: Object-Oriented Programming
Module 5: Advanced Swift
Module 6: Swift and iOS Development
- Introduction to iOS Development
- UIKit Basics
- Storyboards and Interface Builder
- Networking in Swift
- Core Data
- SwiftUI Basics