日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

我的Go+语言初体验——(5)Go+ 基本语法之 Switch

發布時間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 我的Go+语言初体验——(5)Go+ 基本语法之 Switch 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我的Go+語言初體驗——(5)Go+ 基本語法之 Switch

“我的Go+語言初體驗” | 征文活動進行中…

Go+ 語言中提供多路分支條件語句 switch, 用于在不同條件下執行不同動作。
使用 if-else 嵌套結構也可以實現多路分支條件結構,但程序冗長,可讀性差。
本文的例程按照 Go+ 進行了優化和測試,Go+ 的編程風格更加簡潔。


1. Switch 語法:基本語法

switch 是多路分支條件語句, 用于基于不同條件執行不同動作。

使用 if-else 嵌套結構也可以實現多路分支條件結構,但程序冗長,可讀性差。Go+ 提供了更簡練的 Switch 多路分支條件語句,將一個表達式的求值結果與可能的值的列表進行匹配,并根據匹配結果執行相應的代碼。

switch 語句執行的過程從上至下,直到找到匹配項,匹配項后面也不需要再加 break。

Go+ 編程語言中 switch 語句的語法如下:

switch var {case var1:statement(s);case var2:statement(s);// 可以定義任意個數的 casedefault: // Optionalstatement(s); }

變量 var 可以是任何類型,var1, var2 可以是與 var 相同類型的常量、變量或表達式。

switch 語句中可以有表達式,也可以省略。如果 switch 語句中沒有表達式,則默認為 “true”,并對每個 case 表達式求值,執行結果為 “true” 的 case。


2. 【例程1】switch 有表達式

// Example 1: a switch statement with expression dayOfTheWeek := 0 switch dayOfTheWeek { case 1:println "Monday" case 2:println "Tuesday" case 3:println "Wednesday" case 4:println "Thursday" case 5:println "Friday" case 6:println "Saturday" case 0:println "Sunday" }/* Running results: Sunday */

3. 【例程2】switch 無表達式

// Example 2: a switch statement without expression var num int = 80switch { //switch without expression case num < 50:printf "%d < 50\n", num case num < 100:printf "%d < 100\n", num case num < 200:printf "%d < 200", num }/* Running results: 80 < 100 */

程序說明:

  • switch 語句中沒有表達式,則默認為 “true”。對每個 case 表達式求值,執行結果為 “true” 的 case。
  • case 后面可以是常量,也可以是表達式,對 case 表達式求值,根據表達式的結果為 “true” 或 “false” 與 switch 進行匹配。


  • 4. 【例程3】default case

    在 Switch 語句中,關鍵字 default 表示:當沒有其他 case 匹配時,將執行 default 語句。

    顯然,多個 case 和 default 只能執行一個。

    // Example 3: a switch example with default case // When no other case matches, the default statement is executed. dayOfTheWeek := 2 switch dayOfTheWeek { case 0:println "Today is Sunday." case 6:println "Today is Saturday." default:println "Today is a weekday." }/* Running results: Today is a weekday. */

    程序說明:

    dayOfTheWeek := 2,與 switch 中的 case 0,case 6 都不匹配,執行 default 語句。



    5. 【例程4】fallthrough

    在 Go+ 語言中 case 是一個獨立的代碼塊,默認情況下 case 最后自帶 break 語句,匹配成功后就不會執行其他 case。

    為了兼容一些移植代碼,如果需要執行后面的 case,可以使用關鍵字 fallthrough 來實現這一功能。

    fallthrough 必須是 case 語句塊中的最后一條語句。如果它出現在語句塊的中間,編譯器將會報錯。

    新編寫的代碼,不建議使用 fallthrough。

    // Example 4: a switch example with fallthrough var num int = 80println "Switch with fallthrough:" switch { case num < 50:printf "%d < 50\n", numfallthrough case num < 100:printf "%d < 100\n", numfallthrough case num < 200:printf "%d < 200", num }/* Running results: Switch with fallthrough: 80 < 100 80 < 200 */

    程序說明:

  • 【例程4】與【例程2】的區別是 case 語句中的 “fallthrough”。【例程2】 case 語句中沒有 “fallthrough”,程序依次對 case 表達式求值,直到找到匹配項,只要找到匹配項就跳出 switch 語句,而不再執行下一條 case。【例程4】 case 語句中帶有 “fallthrough”,找到匹配項后并不會跳出 switch 語句,繼續執行下一條 case。
  • 如果 switch 沒有表達式則按 “true” 進行匹配。
  • case 中的表達式不必是常量,也可以在運行時被求值。


  • 6. 【例程5】多條件匹配

    在 Go+ 語言中,一個 case 分支中可以包含多個值或多個表達式,每個條件之間用逗號分隔。

    多個值或表達式之間相當于 “與” 的關系,只要匹配其中的一個條件,就執行該 case 的語句。

    // Example 5: a switch example of multiple expressions in case var letter string = "u"switch letter { case "a", "e", "i", "o", "u":printf "%s is a vowel.", letter default:printf "%s isn't a vowel.", letter }/* Running results: u is a vowel. */

    【本節完】


    版權聲明:

    原創作品,轉載必須標注原文鏈接:(https://blog.csdn.net/youcans/article/details/121722748)

    Copyright 2021 youcans, XUPT

    Crated:2021-12-04


    歡迎關注『我的Go+語言初體驗』系列,持續更新中…

    我的Go+語言初體驗——(1)超詳細安裝教程
    我的Go+語言初體驗——(2) IDE 詳細安裝教程
    我的Go+語言初體驗——(3)Go+ 數據類型
    我的Go+語言初體驗——(4)零基礎學習 Go+ 爬蟲
    我的Go+語言初體驗——(5)Go+ 基本語法之 Switch

    “我的Go+語言初體驗” | 征文活動進行中…

    總結

    以上是生活随笔為你收集整理的我的Go+语言初体验——(5)Go+ 基本语法之 Switch的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。