LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • match文
  • enum
  • Exercises

Was this helpful?

  1. コード例で学ぶV言語
  2. examples
  3. section_2

マッチ

match文

match文はif - else文のショートハンドです。 入力がマッチすると、選択肢の中で最初にマッチした文ブロックを実行し、最後の式を返します。 elseのブロックは、他の選択肢とマッチしなかった場合に実行されます。

num := 1
match num % 2 == 0  {
    true { print('The input number is even.') }
    else { print('The input number is odd.') }
}

matchを使うと、条件に応じたさまざまな値で変数を初期化することもできます。

num := 3
s := match num {
    1 { 'one' }
    2 { 'two' }
    else {
        'many'
    }
}

コード例:

fn even(num int) bool {
    match num % 2 == 0  {
        true { print('The input number is even.') }
        else { print('The input number is odd.') }
    }
}

fn num_to_str(num int) int {
    match num {
        1 { 'one' }
        2 { 'two' }
        else {
            'many'
        }
    }
}

fn main() {
    println(even(14))           // 'The input number is even.'
    println(even(3))            // 'The input number is odd.'
    println(num_to_str(1))      // 'one'
    println(num_to_str(2))      // 'two'
    println(num_to_str(352))    // 'many'
}

enum

.項目名構文を用いることで、選択肢でenumの値(enum`の項目名)とマッチさせることもできます。

enum Animal {
    cat
    dog
    goldfish
    pig
}

fn makes_miau(a Animal) bool {
    return match a {
        .cat { true }  // enumの`cat`
        else { false }
    }
}

fn is_land_creature(a Animal) bool {
    return match a {
        .cat { true } // enumの`cat`
        .dog { true } // enumの`dog`
        .pig { true } // enumの`pig`
        else {
            false
        }
    }
}

// 以下の書き方も可能
fn is_land_creature_alt(a Animal) bool {
    return match a {
        .goldfish { false } // enumの`goldfish`
        else {
            true
        }
    }
}

fn main() {
    my_cat := Animal.cat
    my_goldfish := Animal.goldfish

    println(makes_miau(my_cat))             // true
    println(is_land_creature(my_cat))       // true
    println(is_land_creature(my_goldfish))  // false
}

Exercises

  1. 1から50までのすべての偶数の配列を作成するVプログラムを書きましょう。

  2. 数値を持つ配列を渡すと、その中の最大値を返すVプログラムを書きましょう。

  3. color(enum)がredかblueかを調べるVプログラムを書きましょう。

PreviousIf文NextChangelog

Last updated 5 years ago

Was this helpful?