Ada

[ADA]4. ADA 기초(3)

TigerShin 2021. 7. 12. 11:15

오늘은 Ada 기초 세번째 시간이 되겠습니다.

오늘은 Case 문과 Declarative Regions에 대해 알아보겠습니다.

Case Statement

Ada에서의 Case 문은 일반적인 다른 언어에서의 Case 문과 크게 다르지는 않습니다.
우선 Ada에서의 Case 구조를 알아보면, 다음과 같습니다.

case <비교하고자 하는 변수> is
    when <비교하고자 하는 값> =>
        <실행문>
    ...
    when others => -- 기타 값들
        <실햄문>
end case;

기본적으로 when <비교하고자 하는 값> => 을 통하여, 만약 조건에 부합이 된다면, 진입하게 됩니다.
다른 언어와는 유일한 차이점은 바로 Ada에서의 Case 문은 굳이 break가 필요하지 않습니다.

일반적인 언어에서의 case 문에서의 반복문과 같은 성격이 아닌, if-else if-else 문의 성격에 가깝다고 생각하시면 됩니다.

예시로 프로그램을 한번 짜보자면,

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure case_test is
    InputVal : Integer := 0;
begin
    loop
        Put("Enter Value: ");
        Get(InputVal);
        Put(Integer'Image(InputVal) & " is "); --'
        case InputVal is -- Case statement
            when 0 | 1023 =>
                if InputVal = 0 then
                    Put_Line(" Low Value");
                elsif InputVal = 1024 then
                    Put_Line(" Max Value");
                end if;
            when 1..1022 =>
                Put_Line(" Defined Value");
            when others =>
                Put_Line(" Undefined Value");
                exit;
        end case;
    end loop;
end case_test;

저는 이런식으로 짜보았습니다.

간단한 프로그램에 대한 설명을 해보자면, 그냥 InputVal 변수에 값을 받아, case 문을 통하여, 비교를 해보았을 때, 0또는 1023인 경우, 또 그안에서 0인지 1023인지를 비교한 이후에 최댓값 혹은 최솟값임을 표시하도록 합니다.

그 외에 그 사이값일 경우, 정의된 값임을 출력하고, 나머지는 정의되지 않은 값임을 내보내도록 합니다.

Ada에서 Case문에서 하나의 조건이 아니라, or와 같은 역할을 지니게 해보고 싶다면,
when <비교하고자 하는 값> | <비교하고자 하는 값> ... =>
과 같이 or 연산자를 이용하면 됩니다.

위의 프로그램을 잘 실행이 되는지를 확인해보면,

완벽

잘 실행이 됨을 알 수 있습니다.

대략 이정도로 case 문에 대해 간단하게 알아볼 수 있습니다.

Declarative Regions

Declarative Regions은 한국어로 바꾸어보자면, '선언 구역' 또는 '선언 부분' 이라고 생각하시면 됩니다.

어떠한 변수, 타입, 부프로그램 등에 대한 선언을 할 수 있습니다.
Ada에서의 Declarative Regions 부분을 표현해보자면

procedure <프로그램명> is
    -- Declarative Regions
begin
end <프로그램명>;

로, main program을 기준으로 is와 begin 사이에 있습니다.

일반적으로 변수를 선언할 경우 Declarative Regions 부분에 선언합니다.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure <프로그램명> is
    X : Integer;
begin
end <프로그램명>;

으로 할 경우, 정수 변수 X가 탄생하는 것입니다.

부프로그램 같은 경우

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure <프로그램명> is
    procedure <부프로그램명> is
    begin
        <실햄문>
    end <부프로그램명>;
begin
    <부프로그램명>;
end <프로그램명>;

이런식으로 주프로그램을 선언 할 때 처럼 Declarative Regions 부분에 선언을 하시면 됩니다.
그 다음 주프로그램에서 선언한 부프로그램명을 작성할 경우, 호출할 수 있습니다.

또한 이외에도
블럭을 선언하여, 주프로그램에서 지역 변수와 같은 사용을 할 수 있습니다.

블럭 선언은

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure <주프로그램명> is
begin
    declare
        -- Block statement's local variables.
    begin
    end;
end <주프로그램명>;

이런 식으로 가능합니다.

예시로 프로그램을 작성을 해본다면

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure declarative_regions is
begin
    loop
        Put("Enter Your Age: ");
        declare
            Age : Integer := 0;
        begin
            Get(Age);
            exit when Age = -1;
            Put_Line("Your Age is " & Integer'Image(Age) & "!");
        end;
    end loop;
end declarative_regions;

이런 식으로 작성이 가능합니다.

간단한 프로그램 설명을 드리자면, 블럭의 지역변수인 Age에 값을 넣어, Age가 -1이 아닌 경우에는 계속해서 반복하도록 하는 프로그램입니다.

실행을 해보면

잘 됨을 알 수 있습니다.

'Ada' 카테고리의 다른 글

[ADA]Ada 기초(5)  (0) 2021.08.19
[ADA]5. Ada 기초(4)  (0) 2021.08.10
[ADA]3.ADA 기초(2)  (0) 2021.07.05
[ADA]2. ADA 기초(1)  (0) 2021.07.04
[ADA]1.시작  (0) 2021.07.03