If Else - HimxhMC/HimDSL GitHub Wiki

条件分支 / If-Else

if 的基本形式 / The Basic if

if 根据一个条件的真假,决定是否执行某段代码。语法:

if decides whether to run a block based on a condition:

if (条件) {
    语句;
    语句;
}

例子:

Example:

player p = @p;
double hp = p.health;

if (hp < 10) {
    runcmd("CONSOLE", "say 血量告急!");
}

括号里是条件表达式,结果必须是布尔值(或者能转成布尔值的东西,见后文"真值判断")。条件为真执行花括号内,为假跳过。

The parenthesized condition must evaluate to a boolean (or something convertible — see "Truthiness" below). True runs the body; false skips it.


if-else / If-Else

if 后面可以直接跟 else,处理"否则"的情况:

if may be followed by else for the "otherwise" case:

if (hp < 10) {
    runcmd("CONSOLE", "say 需要治疗");
} else {
    runcmd("CONSOLE", "say 状态良好");
}

else 也可以跟另一个 if,形成链式判断:

else may be followed by another if to chain:

if (hp < 5) {
    runcmd("CONSOLE", "say 危急");
} else if (hp < 10) {
    runcmd("CONSOLE", "say 警告");
} else if (hp < 15) {
    runcmd("CONSOLE", "say 注意");
} else {
    runcmd("CONSOLE", "say 健康");
}

else if 会按顺序判断,第一个为真的分支执行后,后面的分支全部跳过。所以顺序很重要:范围宽的判断放前面,会遮住后面的窄判断。

else if evaluates in order: the first true branch runs, and the rest are skipped. Order matters: a wide-range condition placed first will shadow the narrower ones after it.

// ✗ 后面的分支永远不会执行
if (hp < 20) { ... }
else if (hp < 10) { ... }

// ✓ 从窄到宽
if (hp < 10) { ... }
else if (hp < 20) { ... }

花括号的取舍 / Braces

花括号包裹的是一段 block。花括号可以省略,此时 if 只控制紧随其后的一条语句:

Braces wrap a block. They may be omitted, in which case the if controls only the single statement immediately after it:

if (hp < 10)
    runcmd("CONSOLE", "say 血量低");

这种写法很简洁,但也是新手最常踩的坑:多条语句时只有第一条被 if 控制。

The concise form is easy to abuse. With multiple statements, only the first is governed by the if:

if (hp < 10)
    runcmd("CONSOLE", "say 血量低");
    runcmd("CONSOLE", "say 请小心");    // ✗ 这条语句永远执行,不受 if 影响

规则:只要 if / else 后面跟着不止一条语句,就必须加花括号。为了安全,建议一律加。

Rule: as soon as more than one statement follows if / else, braces are required. For safety, use braces everywhere.


嵌套 / Nesting

if 可以嵌套:

if nests:

if (player != null) {
    if (player.health > 0) {
        runcmd("CONSOLE", "say 玩家在线且有血");
    }
}

用 && 也能达到同样效果,且通常更清晰:

The same can be done with &&, usually clearer:

if (player != null && player.health > 0) {
    runcmd("CONSOLE", "say 玩家在线且有血");
}

嵌套层级尽量不要超过三层。超过之后,建议拆成多个函数或提前 return(return 的用法在下一章)。

Avoid nesting more than three levels. Beyond that, split into functions or use early return (covered in the next chapter).


真值判断 / Truthiness

条件表达式的值会被转成布尔值。规则是:

The condition is coerced to boolean. The rules:

  • bool → 原样使用。

  • 数字 → 0 为假,其他为真。

  • null → 假。

  • 字符串 → 非空为真,空字符串为真。

  • bool → used as-is.

  • Numbers → 0 is false, everything else true.

  • null → false.

  • Strings → always true, even "".

关于最后一条要特别注意。Java 程序员容易以为 "" 是假,在 HimDSL 里它是真。

Watch out for the last rule. Java programmers assume "" is false; in HimDSL it is true.

if ("") {            // 结果为真
    runcmd("CONSOLE", "say 空字符串也是真");
}

要判断空字符串请显式比较:

To test for an empty string, compare explicitly:

if (s == "") {
    runcmd("CONSOLE", "say 是空字符串");
}

要判断数字非零,两种写法等价:

To test that a number is non-zero, both forms are equivalent:

if (n != 0) { ... }
if (n) { ... }

推荐用显式比较(n != 0)。虽然多几个字符,但意图清楚,看代码的人不用去回忆真值规则。

Prefer explicit comparisons (n != 0). A few extra characters buys clarity; readers do not have to recall truthiness rules.


常用模式 / Common Patterns

空值保护 / Null Guard

访问对象属性前,先确认对象不是 null:

Before accessing a field on an object, verify it is not null:

player p = @p;
if (p != null) {
    string name = p.name;
    runcmd("CONSOLE", "say 玩家: " + name);
}

@p 在没有在线玩家时返回 null。不加检查直接读 p.name 会报错。

@p returns null if no players are online. Reading p.name without a check errors.

多条件组合 / Combined Conditions

用 && 和 || 组合条件。用括号表达优先级,不要依赖默认规则:

Combine with && and ||. Use parentheses rather than relying on default precedence:

if ((hp < 10 || hungry < 6) && alive) {
    runcmd("CONSOLE", "say 需要治疗");
}

范围判断 / Range Checks

判断数字在某个区间:

Check whether a number is in a range:

if (level >= 10 && level <= 20) {
    runcmd("CONSOLE", "say 等级在 10 到 20 之间");
}

&& 的短路求值在这里没有特别的优势,但保持习惯总没错。

Short-circuit gives no particular advantage here; keeping the habit is still worthwhile.


小结 / Summary

  • if (条件) { ... },else 处理否则分支。

  • else if 按顺序判断,顺序很重要。

  • 多条语句必须加花括号;为安全,建议一律加。

  • 嵌套尽量不超过三层。

  • 真值规则:0、null 为假;"" 为真。

  • 推荐显式比较,少依赖真值规则。

  • if (condition) { ... }; else handles the rest.

  • else if evaluates in order; order matters.

  • Multiple statements need braces; use braces always for safety.

  • Avoid nesting more than three levels.

  • Truthiness: 0 and null are false; "" is true.

  • Prefer explicit comparisons over truthiness.


下一步 / Next Steps

下一章 [for 循环 / For-Loop] 介绍脚本里最常用的重复结构。

Next, [for 循环 / For-Loop)] introduces the most common repetition structure.