with Ada.Text_IO; use Ada.Text_IO; -- Activar Put_Line(String) with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; -- Activar Get(Integer), Put(Integer) procedure Hello is N : Integer; -- Definir variables S : String := " "; -- Ha de ser exactament la longitut que es llegira begin -- Comentari amb doble guió Put_Line("UwU"); -- Print "UwU\n" Get(N); -- Obté enter N Put(N); -- Print ('\t' + N) if N > 100 then -- if Put_Line(" major que 100"); elsif N = 100 then -- else if Put_Line(" és igual a 100"); elsif N = 50 or N = 30 then -- N == 50 || N == 30 Put_Line(" és 50 o és 30"); elsif N = 20 and N > 0 then -- N == 20 && N > 0 Put_Line(" és 20"); elsif N in 0 .. 10 then -- N in range [0, 10] Put_Line(" està entre 0 i 10"); else -- else Put_Line(" menor que 100"); end if; -- final if -> POSAR ; !!!! Put_Line("Has introduit " & Integer'Image (N)); -- Print("Has introduit N")(valor de N) for noDeclarat in 1..3 loop -- for de [1, 3] no fa falta declarar Put_Line("noDeclarat val: " & Integer'Image(noDeclarat)); end loop; -- final loop -> POSAR ; !!!! for noDeclarat in reverse 1..3 loop -- for de [1, 3] no fa falta declarar a l'inversa Put_Line("noDeclarat val: " & Integer'Image(noDeclarat)); end loop; -- final loop -> POSAR ; !!!! N := 0; -- = Se posa amb 2 punts loop -- Equivalent a while(True) N := N + 1; Put(N); exit when N = 5; -- Equivalent a break end loop; -- Acaba bucle Put_Line(""); -- Posa \n N := 0; while N <= 5 loop -- While normal Put_Line (Integer'Image (N)); -- Enters sense tabulació i amb \n N := N + 1; end loop; -- Acaba bucle Get(N); case N is -- Equivalent a switch n when 10 | 20 => Put_Line("N val 10 o 20"); -- Altre forma de or when 100 => Put_Line("N val 100"); when 1..9 => Put_Line("N entre 1 i 9"); when others => Put_Line("N no controlat"); -- Equivalent a default end case; -- Acaba switch loop Put_Line ("Please enter your name: "); declare Name : String := Get_Line; -- Similar a std::getLine(cin, Name); begin exit when Name = ""; Put_Line ("Hi " & Name & "!"); end; -- Name is undefined here end loop; S := Get_Line; Put_Line(S); get(N); Put(N); declare S2 : constant String := (if N > 10 then " major a 10" else " menor a 10"); begin Put_Line(S2); end; end Hello;
Write, Run & Share Ada code online using OneCompiler's Ada online compiler for free. It's one of the robust, feature-rich online compilers for Ada language, running the latest Ada version 2012. Getting started with the OneCompiler's Ada editor is easy and fast. The editor shows sample boilerplate code when you choose language as Ada and start coding.
OneCompiler's Ada online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Ada program which takes name as input and prints hello message with your name.
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
declare
name : String := Ada.Text_IO.Get_Line;
begin
Ada.Text_IO.Put ("Hello ");
Ada.Text_IO.Put_Line (name);
end;
end Hello;
Ada is suitable for all development needs and it is extremely good for developing very large applications with built-in features which supports structured, object-oriented, generic, distributed and concurrent programming directly. Ada was designed by Jean Ichbiah.
It's a Good choice for Rapid Application Development, Extreme Programming. It is a very strong and statically typed language.
This is the simplest loop
begin
Index := 1; --initialization
loop
--code
exit when Index = n;
end loop;
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
Count := 1; --initialization
while Count < n loop
--code
end loop;
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance.
for Index in 1..n loop
--code
end loop;
Ada distinguishes functions and procedures. In simpler terms, functions return some value and must be called as part of larger expressions. Procedures never return a value.
Functions and procedures are collectively called as sub-programs.
procedure proc-name
(X : in Integer ; Y : out Integer ; Z : in out Integer ) is
begin
X := 10; −− it's an Error as you can’t modify an in parameter.
Y := X; −− can modify Y as it's an out parameter.
Z := Z + 1; −− can read and write as it's an in out parameter.
end proc-name;
function function_name(declaration) return value is
begin
--code
end function-name;