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. 例子
  3. section_2

If表达式

if 表达式

if语句是一个编程条件语句,如果被证明为真,则执行块中给定的代码。下面是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执行两个不同的代码。有时,必须从两种以上的可能性中做出选择。if……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…if语句中使用一个if、else或else…if语句。

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.编写一个V程序来接受两个整数并检查它们是否相等。 2.编写一个V程序来检查给定的数字是偶数还是奇数。 3.编写一个V程序来检查给定的数字是正数还是负数。 4.写一个V程序来确定某一年是否是闰年。

Previous循环结构Next匹配

Last updated 5 years ago

Was this helpful?