Basic Syntax and Structure in Swift

Tutorial 5 of 5

1. Introduction

Welcome to this basic tutorial on the syntax and structure of Swift. The goal of this tutorial is to familiarize you with the basic building blocks of Swift, a powerful and intuitive programming language developed by Apple for iOS, macOS, watchOS, and more.

By the end of this tutorial, you will learn about:

  • Variables and constants
  • Data types
  • Control structures (if-else, switch, loops)

Prerequisites: Basic familiarity with programming concepts will be helpful, but not mandatory.

2. Step-by-Step Guide

Variables and Constants

Variables are used to store values. In Swift, you declare a variable using the var keyword. Constants, on the other hand, are declared using the let keyword and their values cannot be changed once set.

var myVariable = 10
let myConstant = 20

Data Types

Swift has several basic data types, including:

  • Int for integers
  • Double for floating-point numbers
  • Bool for Boolean values
  • String for textual data
var myInt: Int = 10
var myDouble: Double = 10.0
var myBool: Bool = true
var myString: String = "Hello, Swift!"

Control Structures

Swift supports common control structures like if-else statements, switch statements, and loops (for-in, while, repeat-while).

// If-Else
if myInt > 5 {
    print("Number is greater than 5")
} else {
    print("Number is 5 or less")
}

// Switch
switch myInt {
case 10:
    print("Number is 10")
default:
    print("Number is not 10")
}

// For-In Loop
for i in 1...5 {
    print(i)
}

3. Code Examples

Let's see a few practical examples.

Example 1: Declare Variables and Constants

var myVariable = 10 // Declaring a variable
myVariable = 20 // Changing the value of the variable

let myConstant = 30 // Declaring a constant
// myConstant = 40 // This will lead to an error as you can't change a constant

Example 2: Using Different Data Types

var myInt: Int = 10 // Integer
var myDouble: Double = 10.0 // Double
var myBool: Bool = true // Boolean
var myString: String = "Hello, Swift!" // String

Example 3: Control Structures

var myInt: Int = 10

// If-Else
if myInt > 5 {
    print("Number is greater than 5") // This will be printed
} else {
    print("Number is 5 or less")
}

// Switch
switch myInt {
case 10:
    print("Number is 10") // This will be printed
default:
    print("Number is not 10")
}

// For-In Loop
for i in 1...5 {
    print(i) // This will print numbers 1 to 5
}

4. Summary

In this tutorial, we covered the basics of Swift's syntax and structure, including variables, constants, data types, and control structures. From here, you can start exploring more complex topics such as functions, classes, and error handling. Check out the official Swift documentation for more information.

5. Practice Exercises

Exercise 1: Declare two integer variables, add them, and print the result.

Solution:

var a: Int = 10
var b: Int = 20
var sum = a + b
print(sum) // Output: 30

Exercise 2: Write a switch statement that prints "Even" for even numbers and "Odd" for odd numbers.

Solution:

var num: Int = 3
switch num % 2 {
case 0:
    print("Even")
default:
    print("Odd") // Output: Odd
}

Exercise 3: Write a for-in loop that prints the first five multiples of 3.

Solution:

for i in 1...5 {
    print(i * 3) // Output: 3 6 9 12 15
}

Keep practicing and exploring more about Swift. Happy Learning!