成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

我們一起聊聊Swift 條件控制和循環

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

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

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

if...else

if-else 語句是一種常見的條件控制結構,用于根據條件的真假執行不同的代碼塊。

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 語句是一種用于處理多個可能情況的流程控制結構。在 Swift 中,switch 語句可以用于處理各種數據類型,包括整數、浮點數、字符串 等。

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 語句用于遍歷集合(如數組、字典或范圍)的循環結構。

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 是一個閉區間運算符,表示一個包括從 1 到 5 的整數范圍。這個范圍包括 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 循環還支持 where 子句,它可以更好地控制循環代碼何時執行。

Swift Code

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

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

while

while 語句是一種用于創建循環的控制流結構,只要給定條件為真,就會反復執行一段代碼塊。

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 語句是一種循環結構,類似于 while 循環,不同之處在于 repeat-while 會先執行一次代碼塊,然后在滿足條件的情況下重復執行。

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 循環會先執行一次代碼塊,然后檢查條件 count <= 5 是否仍然為真。只要條件為真,就會重復執行代碼塊。這確保了至少會執行一次,即使條件一開始就不滿足。

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

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 語句等相關的知識。通過與 TypeScript 語法的對比,希望能幫助您更好地理解 Swift 的相關特性。


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

2024-02-20 21:34:16

循環GolangGo

2023-12-28 09:55:08

隊列數據結構存儲

2023-08-10 08:28:46

網絡編程通信

2023-08-04 08:20:56

DockerfileDocker工具

2023-06-30 08:18:51

敏捷開發模式

2022-05-24 08:21:16

數據安全API

2023-09-10 21:42:31

2023-05-31 08:42:02

管理產品技術項目

2022-04-07 11:43:24

UPnPDLNA協議

2021-08-27 07:06:10

IOJava抽象

2023-05-09 07:51:28

Spring循環依賴

2023-10-31 09:04:21

CPU調度Java

2023-03-26 23:47:32

Go內存模型

2024-07-26 09:47:28

2022-10-08 00:00:05

SQL機制結構

2023-07-24 09:41:08

自動駕駛技術交通

2022-02-23 08:41:58

NATIPv4IPv6

2022-09-22 08:06:29

計算機平板微信

2024-11-28 09:57:50

C#事件發布器

2021-08-12 07:49:24

mysql
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美大片久久久 | 亚洲国产欧美国产综合一区 | 日韩福利视频 | 国产高清视频在线观看播放 | 99免费| 在线观看免费高清av | 国产精品国产三级国产播12软件 | 亚洲福利 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 一区二区在线观看免费视频 | 成人免费三级电影 | 国产精品色 | 亚洲午夜精品一区二区三区他趣 | 中文字幕亚洲一区 | 91av精品| 国产精品中文字幕在线观看 | 日韩三区 | 国产精品区二区三区日本 | 亚洲精品免费看 | 91观看| 亚洲高清一区二区三区 | 久草视频在线播放 | 一区网站 | 蜜桃臀av一区二区三区 | 另类专区成人 | 亚州春色 | 亚洲精品国产成人 | 亚洲一区中文字幕 | 亚洲精品久久久久久久不卡四虎 | 久久久久国产 | 日韩一区二区在线看 | 久久国产精品久久 | 一区二区三区视频在线 | 亚洲最色网站 | 全部免费毛片在线播放网站 | 国产精品久久国产精品 | 性色视频在线观看 | 久草高清视频 | 精品国产91乱码一区二区三区 | a在线观看免费 | 午夜av电影院|