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
  • The if statement
  • The else statement
  • The else if statement
  • Nested if..else statement
  • Using if..else as expression
  • Exercises

Was this helpful?

  1. en
  2. examples
  3. section_2

If statement

The if statement

An if statement is a programming conditional statement that, if proved true, executes the code given in the block. Below is a general example of an if statement in V:

john_height := 100
maria_height := 178

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

In the above code, println() will only execute when the condition is true. There are no parentheses needed for surrounding the condition, and the braces are always required.

The else statement

An else statement is a programming conditional statement in which when if evaluates to false then the code in else block executes.

joey_age := 12
kevin_age := 15

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

In this example, the code inside the else block will execute because the condition in if evaluates to false.

The else if statement

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else if...else ladder allows you to check between multiple test expressions and execute different statements.

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")
}

Output

Tom is younger than Asia

Nested if..else statement

It is always a good practice to nest if...else statements which means you can use one if, else or else...if statement inside another if or else...if statement.

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")
}

Output

tom_age >= 18 and younger than Ashia.

Using if..else as expression

The if..else can also be used as an expression:

tom_age := 20
ashia_age := 38

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

print(s)

Output

Tom is the youngest

Exercises

  1. Write a V program to accept two integers and check whether they are equal or not.

  2. Write a V program to check whether a given number is even or odd.

  3. Write a V program to check whether a given number is positive or negative.

  4. Write a V program to find whether a given year is a leap year or not.

PreviousLooping ConstructsNextMatch

Last updated 5 years ago

Was this helpful?