avatarBytefer

Summarize

Swift Tutorials for Front-end Developers: Conditionals and Loops

Master Swift in 2024 and Start Developing Your Own iOS/macOS App

Welcome to the Mastering Swift tutorial series, in this article we will cover if…else, if…else if…else, switch, and loops in Swift.

Next, we launch Xcode and select “File” > “New” > “Playground”. Create a new Playground and name it “ConditionalsAndLoops”.

if…else

The if…else statement is a common conditional control structure used to execute different blocks of code depending on whether the condition is true or false.

Swift Code

var temperature = 28

if temperature > 30 {
    print("It's a hot day")
} else {
    print("It's not so hot")
}

// Output: It's not so hot

TypeScript Code

let temperature = 28;

if (temperature > 30) {
    console.log("It's a hot day");
} else {
    console.log("It's not so hot");
}

// Output: "It's not so hot"

if…else if…else

The if...else if...else statement allows you to process multiple conditions sequentially.

Swift Code

let score = 85

if score >= 90 {
    print("Excellent!")
} else if score >= 80 {
    print("Good")
} else if score >= 70 {
    print("Average")
} else {
    print("Fail")
}

// Output: Good

TypeScript Code

const score: number = 85;

if (score >= 90) {
    console.log("Excellent!");
} else if (score >= 80) {
    console.log("Good");
} else if (score >= 70) {
    console.log("Average");
} else {
    console.log("Fail");
}

// Output: Good

switch

A switch statement is a flow control structure used to handle multiple possible situations. In Swift, switch statements can be used with a variety of data types, including integers, floats, strings, and more.

Swift Code

let dayOfWeek = "Wednesday"

switch dayOfWeek {
case "Monday":
    print("Start of the workweek")
case "Tuesday", "Wednesday", "Thursday":
    print("Midweek, work in progress")
case "Friday":
    print("It's Friday, almost there!")
case "Saturday", "Sunday":
    print("Weekend vibes")
default:
    print("Invalid day")
}

// Output: Midweek, work in progress

TypeScript Code

const dayOfWeek: string = "Wednesday";

switch (dayOfWeek) {
  case "Monday":
    console.log("Start of the workweek");
    break;
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
    console.log("Midweek, work in progress");
    break;
  case "Friday":
    console.log("It's Friday, almost there!");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend vibes");
    break;
  default:
    console.log("Invalid day");
}

// Output: "Midweek, work in progress" 

for-in

The for-in statement is a looping structure used to iterate over a collection such as an array, dictionary, or range.

Swift Code

for index in 1...5 {
    print("Index is \(index)")
}

/**
 Output:
 Index is 1
 Index is 2
 Index is 3
 Index is 4
 Index is 5
 */

TypeScript Code

for (let index = 1; index <= 5; index++) {
    console.log(`Index is ${index}`);
}

/**
 Output:
 Index is 1
 Index is 2
 Index is 3
 Index is 4
 Index is 5
 */

For-in loops in Swift also support where clauses, which provide greater control over when the looped code is executed.

Swift Code

for index in 1...5 where index % 2 == 0 {
    print("Index is \(index)")
}

/**
 Output:
 Index is 2
 Index is 4
 */

while

The while statement is a control flow structure used to create a loop that repeatedly executes a block of code as long as a given condition is true.

Swift Code

var count = 1

while count <= 5 {
    print("Count is \(count)")
    count += 1
}

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

TypeScript Code

let count: number = 1;

while (count <= 5) {
    console.log(`Count is ${count}`);
    count++;
}

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

repeat-while

The repeat-while statement is a looping construct, similar to a while loop, except that repeat-while executes a block of code once and then repeats it if a condition is met.

Swift Code

var count = 1

repeat {
    print("Count is \(count)")
    count += 1
} while count <= 5

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

In the above code, the repeat-while loop executes the code block once and then checks if the condition count ≤ 5 is still true. As long as the condition is true, the block is repeated. This ensures that it will be executed at least once, even if the condition was not met in the first place.

In TypeScript, there is no equivalent to the repeat-while syntax in Swift. However, you can use do-while loops to achieve similar functionality.

let count: number = 1;

do {
    console.log(`Count is ${count}`);
    count++;
} while (count <= 5);

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

This article introduces if…else, if…else if…else, switch, and loops in Swift. By comparing it with TypeScript syntax, we hope it will help you understand Swift better. If you want to learn Swift, you can follow me on Medium or Twitter to read more about Swift and TS!

Programming
Web Development
JavaScript
iOS
Swift
Recommended from ReadMedium