Examples in V
a := 10
b := 20
if a < b {
	println('$a < $b')
} else if a > b {
	println('$a > $b')
} else {
	println('$a == $b')
}

// if can be used as an expression:
num := 777
s := if num % 2 == 0 {
	'even'
}
else {
	'odd'
}
println(s) // "odd"
Last Run  :
10 < 20
odd
// V has only one looping construct: for.
numbers := [1, 2, 3, 4, 5]
for num in numbers {
	println(num)
}
names := ['Sam', 'Peter']
for i, name in names {
	println('$i) $name')  // Output: 0) Sam
}                         //         1) Peter

// The for value in loop is used for going through elements of an array. If an index is required, an alternative form for index, value in can be used.
// Note, that the value is read-only. If you need to modify the array while looping, you have to use indexing:
mut numbers2 := [1, 2, 3, 4, 5]
for i, num in numbers2 {
	println(num)
	numbers2[i] = 0
}

// This form of the loop is similar to while loops in other languages.
// The loop will stop iterating once the boolean condition evaluates to false.
// Again, there are no parentheses surrounding the condition, and the braces are always required.
mut sum := 0
mut i := 0
for i <= 100 {
	sum += i
	i++
}
println(sum) // "5050"

// The condition can be omitted, this results in an infinite loop.
mut num := 0
for {
	num++
	if num >= 10 {
		break
	}
}
println(num) // "10"

// Finally, there's the traditional C style for loop. It's safer than the `while` form because with the latter it's easy to forget to update the counter and get stuck in an infinite loop.
// Here i doesn't need to be declared with mut since it's always going to be mutable by definition.
for i := 0; i < 10; i++ {
	// Don't print 6
	if i == 6 {
		continue
	}
	println(i)
}
// A match statement is a shorter way to write a sequence of if - else statements.
// When a matching branch is found, the following statement block will be run, and the final expression will be returned.
// The else branch will be evaluated when no other branches match.
os := 'windows'
print('V is running on ')
match os {
	'darwin' { println('macOS.') }
	'linux'  { println('Linux.') }
	else     { println(os) }
}

number := 1
s := match number {
	1    { 'one' }
	2    { 'two' }
	else { 
		println('this works too')
		'many' 
	}
}

// A match statement can also be used to branch on the variants of an enum by using the shorthand .variant_here syntax.
enum Color {
	red
	blue
	green
}
fn is_red_or_blue(c Color) bool {
	return match c {
		.red { true }
		.blue { true }
		else { false }
	}
}
Last Run  :
================ V panic ================
   module: builtin
 function: get()
     file: /home/ubuntu/code_runner/root_volume/v/vlib/builtin/array.v
     line: 153
  message: array.get: index out of range (i == -1, a.len == 0)
=========================================
                                               | 0x56059535eb5a | /code/root/v/v(panic_debug+0x131) 
                                               | 0x56059535d740 | /code/root/v/v(array_get+0xaf) 
                                               | 0x5605953a71c3 | /code/root/v/v(compiler__CGen_insert_before+0x60) 
                                               | 0x5605953cc01b | /code/root/v/v(compiler__Parser_match_statement+0x1c4) 
                                               | 0x56059539477c | /code/root/v/v(compiler__Parser_statement+0x458) 
                                               | 0x56059538fde4 | /code/root/v/v(compiler__Parser_parse+0xe69) 
                                               | 0x5605953d0089 | /code/root/v/v(compiler__V_parse+0x15b) 
                                               | 0x5605953d0306 | /code/root/v/v(compiler__V_compile+0x261) 
                                               | 0x5605953f4eb6 | /code/root/v/v(main__main+0x649) 
                                               | 0x5605953f788b | /code/root/v/v(main+0x68) 
                                               | 0x7fee2618cb97 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) 
                                               | 0x56059535ceea | /code/root/v/v(_start+0x2a) 
// A defer statement defers the execution of a block of statements until the end of scope or until surrounding function returns.
fn read_log() {
	f := os.open('log.txt')
	defer { f.close() }
	// ...
	if !ok {
		// defer statement will be called here, the file will be closed
		return
	}
	// ...
	// defer statement will be called here, the file will be closed
}