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

  1. Comments
  2. Printing to the Console
  3. Semicolons
  4. Variables and Constants
  5. Data Types
  6. Basic Operators
  7. Control Flow Basics

  1. Comments

Comments are used to annotate code and are ignored by the compiler. Swift supports both single-line and multi-line comments.

// This is a single-line comment

/*
 This is a multi-line comment.
 It can span multiple lines.
*/

  1. Printing to the Console

The print function is used to output text to the console.

print("Hello, World!")

  1. 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.

let a = 5; let b = 10; print(a + b)

  1. 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.

var variableName = "Hello"
let constantName = "World"

Example:

var age = 25
let name = "Alice"
print("Name: \(name), Age: \(age)")

  1. 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 integers
  • Double and Float for floating-point numbers
  • Bool for boolean values
  • String for text

Example:

var integer: Int = 10
var floatingPoint: Double = 3.14
var boolean: Bool = true
var text: String = "Swift"

  1. 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:

let andResult = true && false
let orResult = true || false
let notResult = !true

  1. 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

for i in 1...5 {
    print(i)
}

While Loop

var count = 1

while count <= 5 {
    print(count)
    count += 1
}

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.

© Copyright 2024. All rights reserved