如果不在这里复制粘贴我的代码,我如何阻止我的 ADA 程序在运行时执行更多代码行,如果它计算出某个值到“X”?
就像是:
variable_name := variable_name +4;
if variable_name >1 then
// END program here and dont execute any lines under this one
end if
我对编程并不陌生,但对 ADA 并不陌生,因此找到正确的语法很痛苦。有什么帮助吗?
对此没有任何特定的语法。
如果你在主程序中,一个简单的return
就可以了。
与 Ada83 兼容的答案在 SO 上。
只要您没有任何任务,这两个都可以。
有一个 Ada95 Rosetta Code 解决方案,无论您是否有任务都可以使用:
with Ada.Task_Identification; use Ada.Task_Identification;
procedure Main is
-- Create as many task objects as your program needs
begin
-- whatever logic is required in your Main procedure
if some_condition then
Abort_Task (Current_Task);
end if;
end Main;
和一个特定于 GNAT 的解决方案,也适用于任务:
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.OS_Lib;
procedure Stopping is
procedure P is
begin
GNAT.OS_Lib.OS_Exit (0);
end P;
begin
Put_Line ("starting");
P;
Put_Line ("shouldn't have got here");
end Stopping;
if variable_name >1 then
raise PROGRAM_ERROR with "Aborted because ...";
end if;
会做你问的。这是否是你想要的是另一回事,你没有给我们足够的背景来猜测。
“abort”语句也可能有用,但它的正常作用是终止多任务程序中的任务。
引发异常可能是最简单的,如果您不喜欢标准的异常,您可以随时声明自己的异常。除了异常,您还可以在自己的异常处理程序中进行任何整理(例如,如果需要,关闭文件)。有关详细信息,请参阅Wikibook 。