title: “IF Statement” author: “laplante@plcb.ca” date: 2023-05-02 version: “1.0.0” section: “1g”
if — Conditional branching in goweb
In goweb, conditional logic is implemented using the if
statement.
It allows branching execution flow based on the evaluation of logical expressions.
Supported forms:
if logical-expression { ... }
if logical-expression { ... } else { ... }
if logical-expression { ... } else if logical-expression { ... } else { ... }
The expression inside the if
must evaluate to a boolean result.
Execution proceeds into the first matching branch, or into the else
block if no condition matches.
res = {{
i := 5;
if i < 10 { "if is true"; }
}};
// Result:
res = "if is true"
res = {{
i := 5;
if i < 10 { "if is true"; } else { "if is false"; }
}};
// Result:
res = "if is true"
res = {{
i := 5;
if i < 4 { "if is true"; } else { "if is false"; }
}};
// Result:
res = "if is false"
res = {{
i := 5;
if i < 10 {
"if is true";
} else {
"if is false";
}
}};
// Result:
res = "if is true"
res = {{
i := 0;
if i < 10 {
"i < 10";
} else if i < 12 {
"i < 12";
} else {
"i >= 15";
}
i = 11;
if i < 10 {
"i < 10";
} else if i < 12 {
"i < 12";
} else {
"i >= 15";
}
i = 16;
if i < 10 {
"i < 10";
} else if i < 12 {
"i < 12";
} else {
"i >= 15";
}
}};
// Result:
res = "i < 10i < 12i >= 15"
res = {{
i := 11;
if i < 10 {
"i < 10";
} else if i < 12 {
"i < 12";
i := 100;
i;
} else {
"i >= 15";
}
i;
}};
// Result:
res = "i < 1210011"
res = {{
i := 10;
for i > 0 {
i;
if i %! (MISSING)== 0 {
"i="; i; i%!;(MISSING) " is even\n";
} else {
"i="; i; i%!;(MISSING) " is odd\n";
}
i = i - 1;
}
}};
// Result:
res =
10i=100 is even
9i=91 is odd
8i=80 is even
7i=71 is odd
6i=60 is even
5i=51 is odd
4i=40 is even
3i=31 is odd
2i=20 is even
1i=11 is odd