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
  • if文
  • else文
  • else if文
  • if..else文のネスト
  • if..elseを式として扱う
  • 演習

Was this helpful?

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

If文

if文

if文は、プログラミングにおける条件を記述する文であり、trueの場合は指定のブロック内のコードを実行します。以下はVにおけるif文の一般的なコード例です。

john_height := 100
maria_height := 178

if john_height < maria_height {
    println("Maria is taller than John")
}

上のコードのprintln()は、条件がtrueの場合にのみ実行されます。 条件部分を丸かっこで囲む必要はありません。逆に波かっこ{ }は常に必要です。

else文

else文はプログラミングにおける条件を記述する文であり、ifがfalseと評価された場合にelseブロックのコードを実行します。

joey_age := 12
kevin_age := 15

if joey_age > kevin_age {
    println("Joey is older")
} else {
    println("Kevin is older")
}

上のコード例では、elseブロック内のコードが実行されます(ifはfalseと評価されるため)。

else if文

if...else文は、評価式がtrueかfalseかに応じて異なるコードを実行します。しかし可能性のある選択肢が3つ以上になることもあります。if...else if...elseのように重ねることで、複数の評価式をチェックして実行する文を切り替えることができます。

tom_age := 20
ashia_age := 38

if tom_age < ashia_age {
    println("Tom is younger than Ashia")
} else if tom_age > ashia_age {
    println("Tom is older than Ashia")
} else {
    println("Tom and Ashia are the same age")
}

上の出力結果:

Tom is younger than Asia

if..else文のネスト

if...else文をネストすることで、別のif文やelse...if文の中でif文やelse文やelse...if文を使えます。ネストは1回に留めるのがよい習慣です。

tom_age := 20
ashia_age := 38

if tom_age < ashia_age {
    if tom_age < 18 {
        println("tom_age < 18 and younger than Ashia.")
    } else {
        println("tom_age >= 18 and younger than Ashia.")
    }
} else if tom_age > ashia_age {
    println("$tom_age > $ashia_age")
} else {
    println("$tom_age == $ashia_age")
}

上の出力結果:

tom_age >= 18 and younger than Ashia.

if..elseを式として扱う

if..elseは式としても扱えます。

tom_age := 20
ashia_age := 38

s := if tom_age < ashia_age {
    "Tom is the youngest"
} else {
    "Ashia is the youngest"
}

print(s)

上の出力結果:

Tom is the youngest

演習

  1. integerを2つ受け取り、両者が等しいかどうかをチェックするVプログラムを書きましょう。

  2. 渡した数値が奇数か偶数かをチェックするVプログラムを書きましょう。

  3. 渡した数値が正か負かをチェックするVプログラムを書きましょう。

  4. 渡した年がうるう年かどうかをチェックするVプログラムを書きましょう。

PreviousループNextマッチ

Last updated 5 years ago

Was this helpful?