4

Without copy-pasting my code here, how can I stop my ADA program from executing anymore lines of code during run-time if it calculates a certain value to 'X'?

something like:

 variable_name := variable_name +4;
 if variable_name >1 then
 // END program here and dont execute any lines under this one
 end if

I am not new to programming but new to ADA so finding the correct syntax is a pain. Any help?

user2855405
  • 495
  • 2
  • 7
  • 20
  • 1
    Why would anyone want to START the execution of an ADA program? How about 'then return' – Jiminion Feb 12 '15 at 20:35
  • I believe is't the 'abort' statement: http://www.adaic.org/resources/add_content/docs/95style/html/sec_6/6-3-3.html – Gratus D. Feb 12 '15 at 20:35
  • Your example source text does not compile and run. Please show us that you've at least made that much of an effort. – Jacob Sparre Andersen Feb 13 '15 at 08:09
  • @GratusD.The `abort` statement has nothing to do with this. – ajb Feb 14 '15 at 04:50
  • Usually, stopping a program is accomplished by exiting a loop (either by writing a `while` loop that stops when you're done, or by using the `exit` statement to exit a loop). It can also be done by `return` from a procedure that has the main loop in it. If it's normal for your calculation to reach X, then you want to do one of these--please do not try to find a way to do an "emergency exit" from your program, which would be terrible practice. If you really do need an emergency exit, please provide more details so that I can give an appropriate answer. – ajb Feb 14 '15 at 04:58

2 Answers2

8

There isn’t any specific syntax for this.

If you are in the main procedure, a simple return will do.

An Ada83-compatible answer is here on SO.

Both those are OK so long as you don’t have any tasks.

There’s an Ada95 Rosetta Code solution, which will work whether you have tasks or not:

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;

and a GNAT-specific solution, also OK with tasks:

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;
Community
  • 1
  • 1
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
1
 if variable_name >1 then
    raise PROGRAM_ERROR with "Aborted because ...";
 end if;

will do what you ask. Whether that's what you want is another matter, you haven't given us enough context to guess at that.

The "abort" statement might also be usable, but its normal role is terminating tasks within a multi-tasking program.

Raising an exception is probably easiest, and if you don't like the standard ones, you can always declare your own. With an exception you can also do any tidying up (such as closing files if you need to) in your own exception handler. See the Wikibook for more details.