const warn = @import("std").debug.warn;
pub fn main() void {
warn("Hello, world!\n", .{});
}
// Top-level declarations are order-independent
const warn = std.debug.warn;
const std = @import("std");
pub fn main() void {
// different size of integers
const one: i8 = 1;
const two: i16 = 2;
const three: i32 = 3;
const four: i64 = 4;
const five: i128 = 5;
// all of above are also available as unsigned integers.
// [u8, u16, u32, u64]
//different size of floats
const one_and_half: f16 = 1.5;
const two_and_half: f32 = 2.5;
const three_and_half: f64 = 3.5;
const four_and_half: f128 = 4.5;
// Booleans
const truth: bool = true;
const lie: bool = false;
// Strings
// Strings are UTF-8 encoded byte arrays.
const str = "Hello Again !"; // there is type inference ofcourse
}
pub fn main () void {
// const <identifier>: <type> (optional) = value;
const x = 1;
const y: i32 = 2;
y += 1; // compile error, constants are immutable
}
pub fn main () void {
// var <identifier>: <type> (optional) = value;
var i = 2;
var y: i32; // compile error variables should be initialized inline.
}
var i: i32 = 0;
while(i < 20) {
i += 1;
}
var i: i16 = 0;
while(true) {
if (i == 10) break; // use break to break the loop.
if (i == 2) continue; // use continue to jump back to defenition of the loop.
}
While loops has a continue expression which will be executed every time continue has been called.
var i: i32 = 20;
while (i < 20) : (i += 1) {} // (i+=1) get executed every time loop continues, note that there is no continue keyword in our loop but
// every time a loop reaches it's end of body a continue is implicitly called.
In zig, while loops are expressions and the result’s of the expression is the value
of else
branch of the condition that is getting checked every time. break
gets a parameter that is the result
of else branch.
pub fn while_expr() i32 {
var i:i32 = 0;
return while(i < 10) {
i += 1;
if (i == 5) break i; // i is the result of else branch
}
}
pub fn while_expr2() i32 {
var i:i32 = 0;
return while(i < 10) {
i += 1;
} else i; // else branch can be accessed directly, but remember if break is called within the body else body is not evaluated.
}
Labeled loops can be referenced through inner loops.
pub fn testLabel() {
var i:i32 = 1;
outer: while(true) {
while(i<6) {
i+=1;
if (i == 2) {
break :outer;
}
}
}
}
pub fn for_expr() i32 {
const arr = [_]i32{1,2,3};
var sum: i32 = 0;
for (arr) |item| { // for loops can iterate over arrays, slices
sum += item;
}
sum = 0;
for (arr) |item, idx| { // if you want index of each element just capture a second argument.
sum += arr[idx];
}
const result = for (arr) |item| {
} else 2; // like while expressions, for loops can have else expressions which will get executed when for loop exits,
// and again like while expressions if break is called whitin the loop else is not evaluated.
sum = 0;
outer: for (arr) |i| { // for loops can be labeled to be referenced from inner loops
for ([_]i32{4,5,6}) |j| {
sum += j;
if (sum == 9) break :outer;
}
}
}
pub fn test_if() i32 {
const i = 10;
const result1 = if (i == 10) 1 else 2; // if expression can be used instead of ternary operators.
const result2 = if (i == 10) 3 else if (i == 11) 4 else 5; // else if are also allowed in if expressions
if (result1 == result2) {
return 1;
} else if (result1 == 10) {
return 2;
} else {
return 3;
}
pub fn test_if_null() i32 {
const n = null;
if (b) { // if can be used to check for null values
return 1;
} else {
return 2;
}
}
pub fn test_switch() i32 {
return switch (2) {
1,2,3 => 1,
4...10 => 2,
}
}