Pascal:理解 if/then 语句
Pascal: understanding if/then statements
我目前正在移植经典 Moria 游戏的 VMS Pascal 版本,但我不确定我是否理解 if/then 语句的限制(我以前从未用 Pascal 编程过) .
我目前的理解是,如果没有 begin/end,那么 if/then 块只会包含一个后续语句。如果是这样,那么在下面的代码中;
if (i4 > 0) then
with inventory[i4] do
begin
objdes(out_val,i4,false);
msg_print('Your ' + out_val + ' glows faintly!');
if (enchant(toac)) then
begin
flags := uand(%X'7FFFFFFF',flags);
py_bonuses(blank_treasure,0);
end
else
msg_print('The enchantment fails...');
end;
ident := true;
ident := true;
将在 if (tval > 0) then
块之外,这意味着即使 i4
是 0
,ident
仍将设置为 true
.
如果这是正确的,那么是否意味着以下来自 UMoria(C 端口)的代码是错误的?
i_ptr = &inventory[INVEN_WIELD];
if (i_ptr->tval != TV_NOTHING) {
objdes(tmp_str, i_ptr, FALSE);
(void) sprintf(out_val, "Your %s glows faintly!", tmp_str);
msg_print(out_val);
if (enchant(&i_ptr->tohit, 10))
{
i_ptr->flags &= ~TR_CURSED;
calc_bonuses();
}
else
msg_print("The enchantment fails.");
ident = TRUE;
}
...因为 ident = TRUE;
在 if 块内。
我在几个地方看到过类似的例子——我想这些可能是针对 C 端口改变的——但我希望在我改变太多代码之前得到澄清。
您对流量控制的评估是正确的。但是,由于缩进,原始 Pascal 代码中将 indent 赋值给 true 很可能是在 if/then 语句中。
这就是为什么我总是 运行 在 IDE 中对源代码进行自动缩进。它清除了这些错误。 (Python 对此反应过度,因为我已经看到其中的缩进错误并且它不适合自动 IDE 帮助。)
如果有人校对和测试,我怀疑C端口是正确的。
测试驱动开发在这里有所帮助,因为它有助于定义真正的意图。
我目前正在移植经典 Moria 游戏的 VMS Pascal 版本,但我不确定我是否理解 if/then 语句的限制(我以前从未用 Pascal 编程过) .
我目前的理解是,如果没有 begin/end,那么 if/then 块只会包含一个后续语句。如果是这样,那么在下面的代码中;
if (i4 > 0) then
with inventory[i4] do
begin
objdes(out_val,i4,false);
msg_print('Your ' + out_val + ' glows faintly!');
if (enchant(toac)) then
begin
flags := uand(%X'7FFFFFFF',flags);
py_bonuses(blank_treasure,0);
end
else
msg_print('The enchantment fails...');
end;
ident := true;
ident := true;
将在 if (tval > 0) then
块之外,这意味着即使 i4
是 0
,ident
仍将设置为 true
.
如果这是正确的,那么是否意味着以下来自 UMoria(C 端口)的代码是错误的?
i_ptr = &inventory[INVEN_WIELD];
if (i_ptr->tval != TV_NOTHING) {
objdes(tmp_str, i_ptr, FALSE);
(void) sprintf(out_val, "Your %s glows faintly!", tmp_str);
msg_print(out_val);
if (enchant(&i_ptr->tohit, 10))
{
i_ptr->flags &= ~TR_CURSED;
calc_bonuses();
}
else
msg_print("The enchantment fails.");
ident = TRUE;
}
...因为 ident = TRUE;
在 if 块内。
我在几个地方看到过类似的例子——我想这些可能是针对 C 端口改变的——但我希望在我改变太多代码之前得到澄清。
您对流量控制的评估是正确的。但是,由于缩进,原始 Pascal 代码中将 indent 赋值给 true 很可能是在 if/then 语句中。
这就是为什么我总是 运行 在 IDE 中对源代码进行自动缩进。它清除了这些错误。 (Python 对此反应过度,因为我已经看到其中的缩进错误并且它不适合自动 IDE 帮助。)
如果有人校对和测试,我怀疑C端口是正确的。
测试驱动开发在这里有所帮助,因为它有助于定义真正的意图。