跳轉到內容

Ada 程式設計/型別/範圍

來自華夏公益教科書,開放的書籍,開放的世界

Ada. Time-tested, safe and secure.
Ada. 經久耐用,安全可靠。


Arange 是一個帶符號的整數值,其範圍從 First 到最後的 Last。定義如下

 range First .. Last

當將值分配給具有此範圍約束的物件時,會檢查值的有效性,如果值不在 FirstLast 之間,則會引發 Constraint_Error 異常

在聲明範圍型別時,相應的數學運算子會在同一位置由語言隱式宣告。

編譯器可以自由地為此使用者定義的型別選擇合適的底層硬體型別。

工作示例

[編輯 | 編輯原始碼]

以下示例定義一個新的範圍從 -5 到 10,然後打印出整個範圍。

檔案: range_1.adb (檢視, 純文字, 下載頁面, 瀏覽全部)
with Ada.Text_IO;

procedure Range_1 is
   type Range_Type is range -5 .. 10;

   package T_IO renames Ada.Text_IO;
   package I_IO is new Ada.Text_IO.Integer_IO (Range_Type);

begin
   for A in Range_Type loop
      I_IO.Put (
         Item  => A,
         Width => 3,
         Base  => 10);

      if A < Range_Type'Last then
         T_IO.Put (",");
      else
         T_IO.New_Line;
      end if;
   end loop;
end Range_1;

華夏公益教科書

[編輯 | 編輯原始碼]

Ada 參考手冊

[編輯 | 編輯原始碼]
華夏公益教科書