Skip to content

Commit 42d9aa0

Browse files
committed
add if
1 parent 63d2c95 commit 42d9aa0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

if/basic.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const std = @import("std");
2+
3+
pub fn main() void {
4+
const a: u8 = 32;
5+
const b: u8 = 128;
6+
7+
if (a > b) {
8+
std.debug.print("A", .{});
9+
} else if (a < b) {
10+
std.debug.print("B", .{});
11+
} else {
12+
std.debug.print("eq", .{});
13+
}
14+
}

if/optionan.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const std = @import("std");
2+
3+
pub fn main() void {
4+
var a: ?u8 = 32;
5+
check(a);
6+
7+
a = null;
8+
check(a);
9+
}
10+
11+
fn check(a: ?u8) void {
12+
if (a) |value| { // Capture
13+
std.debug.print("Value: {}\n", .{value});
14+
} else {
15+
std.debug.print("Value: null\n", .{});
16+
}
17+
}

if/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# If Expression
2+
3+
```bash
4+
$ zig run basic.zig
5+
B
6+
```
7+
8+
```bash
9+
$ zig run ternary.zig
10+
B
11+
```
12+
13+
```bash
14+
$ zig run optionan.zig
15+
Value: 32
16+
Value: null
17+
```

if/ternary.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
3+
pub fn main() void {
4+
const a: u8 = 32;
5+
const b: u8 = 128;
6+
7+
const c = if (a > b) "A" else "B";
8+
9+
std.debug.print("{s}", .{c});
10+
}

0 commit comments

Comments
 (0)