Repository navigation
Expand file tree
main
/
Copy pathif.zig
More file actions
59 lines (51 loc) · 1.26 KB
/
Copy pathif.zig
File metadata and controls
59 lines (51 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const std = @import("std");
const expect = std.testing.expect;
test "if statement" {
const a = true;
var x: u16 = 0;
if (a) {
x += 1;
} else {
x += 2;
}
try expect(x == 1);
std.debug.print("if statements work\n", .{});
// if statments for optional variables
const optional_var1: ?u8 = null;
const optional_var2: ?u8 = 8;
if (optional_var1) |val| {
std.debug.print("val: {}\n", .{val});
}
if (optional_var2) |val| {
std.debug.print("val: {}\n", .{val});
}
// if you want to return a value from if
// you need to label the block
const num1 = 5;
const if_value = if (num1 == 5) if_label: {
break :if_label 24;
} else 0;
std.debug.print("if_value: {}\n", .{if_value});
// you can do this for errors also
// const myerror = error.SomeError;
// if (myerror) |val| {
// std.debug.print("error: {}", .{val});
// }
}
//const expect = @import("std").testing.expect;
test "if statement 2" {
const a = true;
var x: u16 = 0;
if (a) {
x += 1;
} else {
x += 2;
}
try expect(x == 1);
}
test "if statement expression" {
const a = true;
var x: u16 = 0;
x += if (a) 1 else 2;
try expect(x == 1);
}