自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我們一起聊聊Swift 條件控制和循環(huán)

開發(fā) 前端
本文我們介紹了 Swift 中 if/else, else if, switch 和 loops 語句等相關(guān)的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關(guān)特性。

歡迎您閱讀 Mastering Swift 基礎教程,本文我們將介紹 Swift 中的變量、常量和數(shù)據(jù)類型。如果你尚未安裝 Xcode 和配置 Swift 開發(fā)環(huán)境,請您先閱讀這篇文章。

接下來,我們啟動 Xcode,然后選擇 "File" > "New" > "Playground"。創(chuàng)建一個新的 Playground 并命名為 "ConditionalsAndLoops"。

if...else

if-else 語句是一種常見的條件控制結(jié)構(gòu),用于根據(jù)條件的真假執(zhí)行不同的代碼塊。

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

if...else if...else 語句允許您按順序處理多個條件。

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

switch 語句是一種用于處理多個可能情況的流程控制結(jié)構(gòu)。在 Swift 中,switch 語句可以用于處理各種數(shù)據(jù)類型,包括整數(shù)、浮點數(shù)、字符串 等。

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

相比 JavaScript 和 TypeScript,在 Swift case 分支中,無需使用 break 跳出分支。

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

for-in 語句用于遍歷集合(如數(shù)組、字典或范圍)的循環(huán)結(jié)構(gòu)。

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
 */

在以上代碼中,1...5 是一個閉區(qū)間運算符,表示一個包括從 1 到 5 的整數(shù)范圍。這個范圍包括 1 和 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
 */

在 Swift 中 for-in 循環(huán)還支持 where 子句,它可以更好地控制循環(huán)代碼何時執(zhí)行。

Swift Code

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

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

while

while 語句是一種用于創(chuàng)建循環(huán)的控制流結(jié)構(gòu),只要給定條件為真,就會反復執(zhí)行一段代碼塊。

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

repeat-while 語句是一種循環(huán)結(jié)構(gòu),類似于 while 循環(huán),不同之處在于 repeat-while 會先執(zhí)行一次代碼塊,然后在滿足條件的情況下重復執(zhí)行。

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
 */

以上代碼中,repeat-while 循環(huán)會先執(zhí)行一次代碼塊,然后檢查條件 count <= 5 是否仍然為真。只要條件為真,就會重復執(zhí)行代碼塊。這確保了至少會執(zhí)行一次,即使條件一開始就不滿足。

在 TypeScript 中,目前并沒有對應于 Swift 中 repeat-while 的語法。但可以通過 do-while 循環(huán)來實現(xiàn)類似的功能。

TypeScript Code

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
 */

本文我們介紹了 Swift 中 if/else, else if, switch 和 loops 語句等相關(guān)的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關(guān)特性。


責任編輯:武曉燕 來源: 全棧修仙之路
相關(guān)推薦

2024-02-20 21:34:16

循環(huán)GolangGo

2023-12-28 09:55:08

隊列數(shù)據(jù)結(jié)構(gòu)存儲

2023-05-31 08:42:02

管理產(chǎn)品技術(shù)項目

2022-04-07 11:43:24

UPnPDLNA協(xié)議

2023-05-09 07:51:28

Spring循環(huán)依賴

2023-08-04 08:20:56

DockerfileDocker工具

2022-05-24 08:21:16

數(shù)據(jù)安全API

2023-08-10 08:28:46

網(wǎng)絡編程通信

2023-09-10 21:42:31

2023-06-30 08:18:51

敏捷開發(fā)模式

2021-08-27 07:06:10

IOJava抽象

2023-10-31 09:04:21

CPU調(diào)度Java

2023-10-31 08:10:24

域名域名解析服務器

2022-02-23 08:41:58

NATIPv4IPv6

2022-09-22 08:06:29

計算機平板微信

2024-07-26 09:47:28

2021-08-12 07:49:24

mysql

2023-03-26 23:47:32

Go內(nèi)存模型

2024-11-28 09:57:50

C#事件發(fā)布器

2022-10-08 00:00:05

SQL機制結(jié)構(gòu)
點贊
收藏

51CTO技術(shù)棧公眾號