4.2. Case - JulTob/Ada GitHub Wiki

Case

Unlike C, Ada does not fall without a break. All cases that match are executed.

You have to cover all possible values in the type.

case x_type'Pos(x) is 
   when 1 =>   
      --.1.--
   when 2..4 =>   
      --.2,3,4.--
   when 5 | 7 =>   
      --.5,7.--
   when 6 | 8..10 =>   
      --.6,8,9,10.--
   when others =>   
      null;
end case;   
-- Letter : Character;
case Letter is 
   when ‘i’ =>   
      --.i.--
   when ‘A’|’a’ =>   
      --.A.--
   when ‘E’|’e’|’I’|’o’ =>   
      --.*.--
   when others =>   
      null;
end case;  
-- Number : Integer;
case Number is 
   when 0 =>   
      --.0.--
   when 1 .. 10 =>   
      --.1.--
   when 5 =>   
      --.5. ,and .1. also runs--
   when others =>   
      null;
end case;
-- Day : Day_t;
case Day is 
   when Sun =>   
      GoTo Church;
   when Mon..Fri =>   
      GoTo Work;
   when Sat =>   
      GoTo Synagogue;
   when Fri | Sat =>   
      GoTo Party;
end case;  
--This is a funny Example. Don't use GoTo like that

-- Number : Integer;
case Number is 
   when Positive =>   
      Put("is positive, ");
   when Natural =>   
      Put("is natural, ");
   when (Number res 2 = 0) =>   
      Put("is even, ");
   when others =>   
      New_Line;
end case;
Put_Line(" and a number.")

with Ada.Text_IO;
use Ada.Text_IO;

with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure Vowels is 
   Letter: Character;
   VowelCount: Integer := 0;
   Y_Count: Integer := 0;
begin 
   while not End_Of_File
   loop 
      Get(Letter);
      case Letter is
        when ’A’|’E’|’ I ’|’ O’|’ U’|’ a’|’ e ’|’ i ’|’ o’|’ u’ =>
           VowelCount := VowelCount + 1;
        when’Y’|’y ’ => 
           Y_Count := Y_Count + 1;
        when others => null;
      end case;
   end loop;
   Put(”Total number of vowels = ”); 
   Put(VowelCount); 
   NewLine;
   Put(”Total number of Ys = ”); 
   Put(Y_Count);  
   New_Line;
end Vowels;
  type Days is (Monday, Tuesday, Wednesday, Thursday,
    Friday, Saturday, Sunday);
 Day: Days;
...

case Today is
    when Monday =>
      Ada.Text_IO.Put_Line("Today is Monday.");
    when Tuesday =>
      Ada.Text_IO.Put_Line("Today is Tuesday.");
    when Wednesday | Thursday | Friday =>
      Ada.Text_IO.Put_Line(
        "Today is either Wednesday, Thursday or Friday.");
        when Saturday | Sunday =>
      Ada.Text_IO.Put_Line(
        "Today is either Saturday or Sunday.");
    when others =>
      Ada.Text_IO.Put_Line("I don't know what today is.");
  end case;

Una sentencia case selecciona la ejecución alternativa entre varias secuencias de sentencias. La elección de la sentencia se realiza en función del resultado de una expresión que debe ser de un tipo discreto. La secuencia de sentencias que se ejecutará es la que tenga asociada la discrete_choice que coincida con el resultado de la expresión (o la asociada con la opción others, si el resultado de la expresión no casa con ninguna otra de las opciones).

case Mes is
   when 1 | 3 | 5 | 7 | 8 | 10 | 12 => Días := 31;
   when 2 => Días := 28;
   when others => Días := 30;
   end case;
case Día is
   when 1 .. 5 => Put_Line ("Trabajo");
   when 6 | 7  => Put_Line ("Descanso");
   end case;